From cea949a2c4243a7110cfcacfbc4698911dda3715 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 24 Sep 2011 23:13:38 +1000 Subject: [PATCH] Enhancements to group handling --- application/classes/controller/lnapp/tree.php | 4 +- application/classes/controller/tree.php | 67 ++++++++++--------- application/classes/module/method.php | 53 --------------- modules/account/classes/auth/osb.php | 7 +- modules/account/classes/model/account.php | 8 +-- modules/group/classes/model/group.php | 27 ++++++++ 6 files changed, 70 insertions(+), 96 deletions(-) delete mode 100644 application/classes/module/method.php diff --git a/application/classes/controller/lnapp/tree.php b/application/classes/controller/lnapp/tree.php index 02c8629f..fc49ee84 100644 --- a/application/classes/controller/lnapp/tree.php +++ b/application/classes/controller/lnapp/tree.php @@ -81,9 +81,9 @@ $(function () { * The incoming ID is either a Branch B_x or a Node N_x * Where X is actually the module. * - * @param id + * @param array $data Tree data passed in by inherited methods */ - public function action_json($id=null,array $data=array()) { + public function action_json(array $data=array()) { if ($this->_auth_required() AND ! Auth::instance()->logged_in()) { $this->output = array('attr'=>array('id'=>'a_login'), 'data'=>array('title'=>_('Please Login').'...','attr'=>array('id'=>'N_login','href'=>URL::site('/login')))); diff --git a/application/classes/controller/tree.php b/application/classes/controller/tree.php index e7132397..fbf61f2a 100644 --- a/application/classes/controller/tree.php +++ b/application/classes/controller/tree.php @@ -19,52 +19,57 @@ class Controller_Tree extends Controller_lnApp_Tree { * The incoming ID is either a Branch B_x or a Node N_x * Where X is actually the module. * - * @param id + * @param array $data Tree data passed in by inherited methods */ - public function action_json($id=NULL,array $data=array()) { + public function action_json(array $data=array()) { // Get the user details - $id = (is_null($id) AND isset($_REQUEST['id'])) ? substr($_REQUEST['id'],2) : $id; + $id = (is_null($this->request->param('id')) AND isset($_REQUEST['id'])) ? substr($_REQUEST['id'],2) : $this->request->param('id'); $user = Auth::instance()->get_user(); if ($user) { if (! $id) { $modules = array(); foreach ($user->groups() as $go) - $modules = array_merge($modules,Module_Method::groupmodules($go->id)); + foreach ($go->list_parentgrps(TRUE) as $cgo) + foreach ($cgo->module_method->find_all() as $mmo) + if ($mmo->menu_display AND empty($modules[$mmo->module_id])) + $modules[$mmo->module_id] = $mmo->module; - ksort($modules); + Sort::masort($modules,'name'); - $data = array(); - foreach ($modules as $module => $details) - if (! $details['parent_id']) - array_push($data, - array('id'=>$details['id'],'name'=>$module,'state'=>'closed') - ); + foreach ($modules as $id => $mo) + if (! $mo->parent_id) + array_push($data,array('id'=>$id,'name'=>$mo->name,'state'=>'closed')); } else { - $module = preg_replace('/^N_/','',$id); + $mo = ORM::factory('module',$id); + $methods = array(); - foreach ($user->groups() as $go) - $methods = array_merge($methods,Module_Method::groupmethods($go->id,$module)); + if ($mo->loaded()) { + foreach ($mo->module_method->find_all() as $mmo) + if ($mmo->menu_display) + foreach ($mmo->group->find_all() as $gmo) + if ($user->has_any('group',$gmo->list_childgrps(TRUE))) + $methods[$mmo->id] = $mmo; - ksort($methods); + Sort::masort($modules,'name'); - $data = array(); - foreach ($methods as $method => $details) { - if (preg_match('/_/',$method)) { - list($mode,$action) = explode('_',$method); - $url = URL::site(sprintf('/%s/%s/%s',$mode,$details['module'],$action)); - } else { - $url = URL::site(sprintf('/%s/%s',$details['module'],$method)); + foreach ($methods as $id => $mmo) { + if (preg_match('/_/',$mmo->name)) { + list($mode,$action) = explode('_',$mmo->name); + $url = URL::site(sprintf('/%s/%s/%s',$mode,$mmo->module->name,$action)); + } else { + $url = URL::site(sprintf('/%s/%s',$mmo->module->name,$mmo->name)); + } + + array_push($data,array( + 'id'=>sprintf('%s_%s',$mmo->module_id,$id), + 'name'=>$mmo->name, + 'state'=>'none', + 'attr_id'=>sprintf('%s_%s',$mmo->module->name,$id), + 'attr_href'=>(empty($details['page']) ? $url : $details['page']) + )); } - - array_push($data,array( - 'id'=>sprintf('%s_%s',$module,$details['id']), - 'name'=>$method, - 'state'=>'none', - 'attr_id'=>sprintf('%s_%s',$module,$details['id']), - 'attr_href'=>(empty($details['page']) ? $url : $details['page']) - )); } } } @@ -81,7 +86,7 @@ class Controller_Tree extends Controller_lnApp_Tree { ); } - return parent::action_json($id,$data); + return parent::action_json($data); } } ?> diff --git a/application/classes/module/method.php b/application/classes/module/method.php deleted file mode 100644 index 3a1e38b9..00000000 --- a/application/classes/module/method.php +++ /dev/null @@ -1,53 +0,0 @@ -param(':siteid',Config::siteid()) - ->param(':gid',$gid); - - foreach ($query->execute() as $record) { - $modules[$record['module']]['id'] = $record['MOD_ID']; - $modules[$record['module']]['parent_id'] = $record['PARENT_ID']; - $modules[$record['module']]['parent'] = $record['parent']; - } - - return $modules; - } - - /** - * Display the methods available for a group - */ - public static function groupmethods($gid,$mid) { - $methods = array(); - // @todo the database prefix needs to be added to this query - $query = DB::query(Database::SELECT,'SELECT C.id,C.name AS METHOD,A.name AS MODULE,C.page FROM ab_module A, ab_module_method C, ab_group_method D WHERE A.id=C.module_id AND A.site_id=C.site_id AND D.method_id=C.id AND D.site_id=C.site_id AND D.group_id=:gid AND C.module_id=:mid AND C.menu_display=1 AND A.site_id=:siteid') - ->param(':siteid',Config::siteid()) - ->param(':gid',$gid) - ->param(':mid',$mid); - - foreach ($query->execute() as $record) { - $methods[$record['METHOD']]['id'] = $record['id']; - $methods[$record['METHOD']]['page'] = $record['page']; - $methods[$record['METHOD']]['module'] = $record['MODULE']; - } - - return $methods; - } -} -?> diff --git a/modules/account/classes/auth/osb.php b/modules/account/classes/auth/osb.php index d241da62..0459515f 100644 --- a/modules/account/classes/auth/osb.php +++ b/modules/account/classes/auth/osb.php @@ -67,12 +67,11 @@ class Auth_OSB extends Auth_ORM { foreach ($gmo->find_all() as $gm) { $roles .= ($roles ? '|' : '').$gm->group->name; - $ro = ORM::factory('group', array('name' => $gm->group->name)); - - // $ro->id == 0 means all users. - if ($ro->id == 0 OR $user->has('group', $ro)) { + // $gm->group->id == 0 means all users. + if ($gm->group->id == 0 OR $user->has_any('group',$gm->group->list_childgrps(TRUE))) { $status = TRUE; $roles = ''; + break; } } diff --git a/modules/account/classes/model/account.php b/modules/account/classes/model/account.php index 37a08da5..cbdefa1b 100644 --- a/modules/account/classes/model/account.php +++ b/modules/account/classes/model/account.php @@ -66,18 +66,14 @@ class Model_Account extends Model_Auth_UserDefault { * Get the groups that an account belongs to */ public function groups() { - return $this->group->find_all()->as_array(); + return $this->group->find_all(); } public function admin() { // @todo Define admins in the config file or DB $admins = array('Root'); - foreach ($this->groups() as $go) - if (in_array($go->name,$admins)) - return TRUE; - - return FALSE; + return $this->has($admins); } /** diff --git a/modules/group/classes/model/group.php b/modules/group/classes/model/group.php index a8ecc92c..d9eaa366 100644 --- a/modules/group/classes/model/group.php +++ b/modules/group/classes/model/group.php @@ -12,6 +12,7 @@ class Model_Group extends Model_Auth_RoleDefault { // Relationships protected $_has_many = array( 'account'=>array('through'=>'account_group'), + 'module_method'=>array('through'=>'group_method','far_key'=>'method_id'), ); protected $_sorting = array( @@ -36,6 +37,10 @@ class Model_Group extends Model_Auth_RoleDefault { ), ); + /** + * This function will, given a group, list all of the children that + * are also related to this group, in the group heirarchy. + */ public function list_childgrps($incParent=FALSE) { $return = array(); @@ -53,5 +58,27 @@ class Model_Group extends Model_Auth_RoleDefault { return $return; } + + /** + * This function will, given a group, list all of the parent that + * are also related to this group, in the group heirarchy. + */ + public function list_parentgrps($incParent=FALSE) { + $return = array(); + + if (! $this->loaded()) + return $return; + + foreach (ORM::factory('group')->where('status','=',1)->and_where('id','=',$this->parent_id)->find_all() as $go) { + array_push($return,$go); + + $return = array_merge($return,$go->list_parentgrps()); + } + + if ($incParent) + array_push($return,$this); + + return $return; + } } ?>