From 77f21ece61bdeebf81cd7fc18f53956f735b8555 Mon Sep 17 00:00:00 2001 From: Ibnu Maksum Date: Thu, 12 Oct 2023 13:32:45 +0700 Subject: [PATCH] Fix select router --- system/autoload/Radius.php | 220 +++++++++++-------------------------- ui/ui/autoload-server.tpl | 6 +- 2 files changed, 67 insertions(+), 159 deletions(-) diff --git a/system/autoload/Radius.php b/system/autoload/Radius.php index 7231d3d9..3261eeb3 100644 --- a/system/autoload/Radius.php +++ b/system/autoload/Radius.php @@ -68,115 +68,63 @@ class Radius return $n->save(); } - public static function planAdd($plan_id, $rate, $pool = null) + public static function planUpSert($plan_id, $rate, $pool = null) { $rates = explode('/', $rate); - $r = Radius::getTablePackage()->create(); - $r->groupname = "plan_".$plan_id; - $r->attribute = 'Ascend-Data-Rate'; - $r->op = ':='; - $r->value = $rates[1]; - $r->plan_id = $plan_id; - if ($r->save()) { - $r = Radius::getTablePackage()->create(); - $r->groupname = "plan_".$plan_id; - $r->attribute = 'Ascend-Xmit-Rate'; - $r->op = ':='; - $r->value = $rates[0]; - $r->plan_id = $plan_id; - if ($r->save()) { - if ($pool != null) { - $r = Radius::getTablePackage()->create(); - $r->groupname = "plan_".$plan_id; - $r->attribute = 'Framed-Pool'; - $r->op = ':='; - $r->value = $pool; - $r->plan_id = $plan_id; - if ($r->save()) { - return true; - } - } else { - return true; - } - } + Radius::upsertPackage($plan_id, 'Ascend-Data-Rate', $rates[1], ':='); + Radius::upsertPackage($plan_id, 'Ascend-Xmit-Rate', $rates[0], ':='); + if ($pool != null) { + Radius::upsertPackage($plan_id, 'Framed-Pool', $pool, ':='); } - return false; } - public static function planUpdate($plan_id, $rate, $pool = null) + public static function planDelete($plan_id) { - $rates = explode('/', $rate); - if (Radius::getTablePackage()->where_equal('plan_id', $plan_id)->find_one()) { - $r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Ascend-Data-Rate')->findOne(); - $r->groupname = "plan_".$plan_id; - $r->value = $rates[1]; - if ($r->save()) { - $r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Ascend-Xmit-Rate')->findOne(); - $r->groupname = "plan_".$plan_id; - $r->value = $rates[0]; - if ($r->save()) { - if ($pool != null) { - $r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', 'Framed-Pool')->findOne(); - $r->groupname = "plan_".$plan_id; - $r->value = $pool; - if ($r->save()) { - return true; - } - } else { - return true; - } - } - } - } else { - if (!empty($plan_id)) { - return Radius::planAdd($plan_id, $rate, $pool); - } - } - return false; - } - - public static function planDelete($plan_id){ // Delete Plan - Radius::getTablePackage()->where_equal('plan_id', "plan_".$plan_id)->delete_many(); + Radius::getTablePackage()->where_equal('plan_id', "plan_" . $plan_id)->delete_many(); // Reset User Plan - $c = Radius::getTableUserPackage()->where_equal('groupname', "plan_".$plan_id)->findMany(); + $c = Radius::getTableUserPackage()->where_equal('groupname', "plan_" . $plan_id)->findMany(); if ($c) { - foreach($c as $u){ + foreach ($c as $u) { $u->groupname = ''; $u->save(); } } } - public static function customerChangeUsername($from, $to){ + + public static function customerChangeUsername($from, $to) + { $c = Radius::getTableCustomer()->where_equal('username', $from)->findMany(); if ($c) { - foreach($c as $u){ + foreach ($c as $u) { $u->username = $to; $u->save(); } } $c = Radius::getTableUserPackage()->where_equal('username', $from)->findMany(); if ($c) { - foreach($c as $u){ + foreach ($c as $u) { $u->username = $to; $u->save(); } } } - public static function customerDeactivate($username){ + public static function customerDeactivate($username) + { global $radius_pass; $r = Radius::getTableCustomer()->where_equal('username', $username)->whereEqual('attribute', 'Cleartext-Password')->findOne(); - if($r){ + if ($r) { // no need to delete, because it will make ID got higher // we just change the password - $r->value = md5(time().$username.$radius_pass); + $r->value = md5(time() . $username . $radius_pass); $r->save(); } } - public static function customerDelete($username){ + public static function customerDelete($username) + { Radius::getTableCustomer()->where_equal('username', $username)->delete_many(); Radius::getTableUserPackage()->where_equal('username', $username)->delete_many(); } @@ -184,17 +132,18 @@ class Radius /** * When add a plan to Customer, use this */ - public static function customerAddPlan($customer, $plan){ - if(Radius::customerAdd($customer, $plan)){ + public static function customerAddPlan($customer, $plan) + { + if (Radius::customerUpsert($customer, $plan)) { $p = Radius::getTableUserPackage()->where_equal('username', $customer['username'])->findOne(); if ($p) { // if exists - $p->groupname = "plan_".$plan['id']; + $p->groupname = "plan_" . $plan['id']; return $p->save(); - }else{ + } else { $p = Radius::getTableUserPackage()->create(); $p->username = $customer['username']; - $p->groupname = "plan_".$plan['id']; + $p->groupname = "plan_" . $plan['id']; $p->priority = 1; return $p->save(); } @@ -202,88 +151,47 @@ class Radius return false; } - public static function customerAdd($customer, $plan) + public static function customerUpsert($customer, $plan) { - if (Radius::getTableCustomer()->where_equal('username', $customer['username'])->findOne()) { - // Edit if exists - $r = Radius::getTableCustomer()->where_equal('username', $customer['username'])->whereEqual('attribute', 'Cleartext-Password')->findOne(); - if($r){ - if($plan['type']=='PPPOE'){ - if(empty($customer['pppoe_password'])){ - $r->value = $customer['password']; - }else{ - $r->value = $customer['pppoe_password']; - } - }else{ - $r->value = $customer['password']; - } - $r->save(); - }else{ - $r = Radius::getTableCustomer()->create(); - $r->username = $customer['username']; - $r->attribute = 'Cleartext-Password'; - $r->op = ':='; - if($plan['type']=='PPPOE'){ - if(empty($customer['pppoe_password'])){ - $r->value = $customer['password']; - }else{ - $r->value = $customer['pppoe_password']; - } - }else{ - $r->value = $customer['password']; - } - $r->save(); - } - $r = Radius::getTableCustomer()->where_equal('username', $customer['username'])->whereEqual('attribute', 'Simultaneous-Use')->findOne(); - if($r){ - if($plan['type']=='PPPOE'){ - $r->value = 1; - }else{ - $r->value = $plan['shared_users']; - } - $r->save(); - }else{ - $r = Radius::getTableCustomer()->create(); - $r->username = $customer['username']; - $r->attribute = 'Simultaneous-Use'; - $r->op = ':='; - if($plan['type']=='PPPOE'){ - $r->value = 1; - }else{ - $r->value = $plan['shared_users']; - } - $r->save(); - } - return true; + if ($plan['type'] == 'PPPOE') { + Radius::upsertCustomer($customer['username'], 'Cleartext-Password', (empty($customer['pppoe_password'])) ? $customer['password'] : $customer['pppoe_password']); } else { - // add if not exists - $r = Radius::getTableCustomer()->create(); - $r->username = $customer['username']; - $r->attribute = 'Cleartext-Password'; - $r->op = ':='; - if($plan['type']=='PPPOE'){ - if(empty($customer['pppoe_password'])){ - $r->value = $customer['password']; - }else{ - $r->value = $customer['pppoe_password']; - } - }else{ - $r->value = $customer['password']; - } - if ($r->save()) { - $r = Radius::getTableCustomer()->create(); - $r->username = $customer['username']; - $r->attribute = 'Simultaneous-Use'; - $r->op = ':='; - if($plan['type']=='PPPOE'){ - $r->value = 1; - }else{ - $r->value = $plan['shared_users']; - } - $r->save(); - return true; - } + Radius::upsertCustomer($customer['username'], 'Cleartext-Password', $customer['password']); } + Radius::upsertCustomer($customer['username'], 'Simultaneous-Use', ($plan['type'] == 'PPPOE')? 1: $plan['shared_users'] ); return false; } + + /** + * To insert or update existing plan + */ + private static function upsertPackage($plan_id, $attr, $value, $op = ':=') + { + $r = Radius::getTablePackage()->where_equal('plan_id', $plan_id)->whereEqual('attribute', $attr)->find_one(); + if (!$r) { + $r = Radius::getTablePackage()->create(); + $r->groupname = "plan_" . $plan_id; + $r->plan_id = $plan_id; + } + $r->attribute = $attr; + $r->op = $op; + $r->value = $value; + return $r->save(); + } + + /** + * To insert or update existing customer + */ + private static function upsertCustomer($username, $attr, $value, $op = ':=') + { + $r = Radius::getTableCustomer()->where_equal('username', $username)->whereEqual('attribute', $attr)->find_one(); + if (!$r) { + $r = Radius::getTableCustomer()->create(); + $r->username = $username; + } + $r->attribute = $attr; + $r->op = $op; + $r->value = $value; + return $r->save(); + } } diff --git a/ui/ui/autoload-server.tpl b/ui/ui/autoload-server.tpl index 4bb7e40a..6bd70fe6 100644 --- a/ui/ui/autoload-server.tpl +++ b/ui/ui/autoload-server.tpl @@ -1,7 +1,7 @@ +{if $_c['radius_enable']} + +{/if} {foreach $d as $ds} - {if $_c['radius_enable']} - - {/if} {/foreach} \ No newline at end of file