Enhancements to group handling

This commit is contained in:
Deon George 2011-09-24 23:13:38 +10:00
parent 7180e01dcf
commit cea949a2c4
6 changed files with 70 additions and 96 deletions

View File

@ -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'))));

View File

@ -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);
}
}
?>

View File

@ -1,53 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides access to module configuration.
*
* @package OSB
* @subpackage Module
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Module_Method {
/**
* Display the modules available for a group
*/
public static function groupmodules($gid) {
$modules = array();
// @todo the database prefix needs to be added to this query
$query = DB::query(Database::SELECT,'SELECT A.name AS module,A.id AS MOD_ID,B.name AS parent,B.id AS PARENT_ID FROM ab_module A LEFT JOIN ab_module B ON (A.parent_id=B.id AND A.site_id=B.site_id), 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 A.menu_display=1 AND A.site_id=:siteid GROUP BY module')
->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;
}
}
?>

View File

@ -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;
}
}

View File

@ -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);
}
/**

View File

@ -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;
}
}
?>