function add() { if (empty($this->data)) { $this->render(); } else { if ($this->Member->findByUsername($this->data['Member']['username'])) { $this->Member->invalidate('username'); $this->set('username_error', '用户名已经存在!'); } else { $this->cleanUpFields(); $this->data['Member']['password'] = md5($this->data['Member']['password']); $member_alias = $this->data['Member']['username']; if ($this->Member->save($this->data)) { $aro = new Aro(); $aro->create($this->Member->uid, 'Members', $this->data['Member']['username']); //把新增用户添加到"Members"组中 $aco = new Aco(); $aco->create($member_id, 3, $member_alias); $this->Acl->allow('Admins', $member_alias, '*'); $this->Acl->allow($this->Session->read('User.id'), $member_alias, '*'); $this->Session->setFlash('添加成功!'); $this->redirect('/teachers/index'); } else { $this->Session->setFlash('添加用户出错!'); } } } }
function isAuthorized($user, $controller, $action) { $aco = new Aco(); $resource = $aco->findByAlias($controller . "::" . $action); if (!empty($resource)) { return $this->Acl->check($user, $controller . "::" . $action, '*'); } else { return $this->Acl->check($user, $controller, 'read'); } }
/** * add method * * @return void */ public function add() { if ($this->request->is('post')) { $this->Page->create(); if ($this->Page->save($this->request->data)) { //Edita o Model e o foreign_key do aco corresponente ao seu Page. $aco = new Aco(); $aco->create(); $aco_options = array('id' => $this->request->data['Page']['aco_id'], 'model' => 'Page', 'foreign_key' => $this->Page->id); $aco->save($aco_options); $this->Session->setFlash('Acesso controlado salvo com sucesso!', 'alert_success'); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash('O Acesso controlado não pode ser salvo. Tente novamente!', 'alert_error'); } } }
/** * admin_index * * @param id integer aco id, when null, the root ACO is used * @return void */ function index($id = null, $level = null) { $aco = new Aco(); // $this->data = $aco->generateTreeList(null, null, null, '&a; '); // if (isset($this->request->query['root'])) { // $query = strtolower($this->request->query['root']); // } // // if ($id == null) { // $root = isset($query) ? $query : 'controllers'; // //$root = $this->AclAco->node(str_replace('.', '_', $root)); // $root = $aco->node(str_replace('.', '_', $root)); // $root = $root[0]; // } else { // $root = $aco->read(null, $id); // } //pr($root); //die; // if ($level !== null) { // $level++; // } // $acos = array(); $roles = $this->Groups->find('list'); // if ($root) { // $fields = Hash::merge(array('id', 'parent_id', 'alias'), $fields = array()); //// $acos = $this->children($acoId, true, $fields); // $acos = $aco->children($root['Aco']['id'], true, $fields); // //// foreach ($acos as $key => $acov) { //// $children = $aco->childCount($acov['Aco']['id'], true); //// pr($acos); //// pr($acos[$key]['Aco']); //// $acos[$key]['Aco']['children'][] = $children; //// } // // } // $options = array('conditions' => array('Aco.parent_id' => 1)); $acos = $aco->find('all', $options = null); // pr($roles); // $this->set(compact('acos', 'roles', 'level')); $this->set(compact('acos', 'roles')); // pr($level);die; // $acos = $this->AclAco->getChildren($root['Aco']['id']); }
static function check($requiredPriv = null, $otherPriv = null) { if (!is_array($requiredPriv)) { $requiredPriv = array($requiredPriv); } /* Adujsting access control object from plugins */ $requiredPriv = Aco::adjust($requiredPriv, $otherPriv); /* Adujsting required object from plugins by requiredPriv*/ $currentPriv = Privilege::adjust($requiredPriv); /* We need one of requiredPriv elements is in currentPriv array */ foreach ($requiredPriv as $obj) { if (in_array($obj, $currentPriv)) { return true; } } return false; }
/** * test After Save * * @return void * @access public */ function testAfterSave() { $Post =& new AclPost(); $data = array('Post' => array('author_id' => 1, 'title' => 'Acl Post', 'body' => 'post body', 'published' => 1)); $Post->save($data); $result = $this->Aco->find('first', array('conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id))); $this->assertTrue(is_array($result)); $this->assertEqual($result['Aco']['model'], 'Post'); $this->assertEqual($result['Aco']['foreign_key'], $Post->id); $aroData = array('Aro' => array('model' => 'AclPerson', 'foreign_key' => 2, 'parent_id' => null)); $this->Aro->save($aroData); $Person =& new AclPerson(); $data = array('AclPerson' => array('name' => 'Trent', 'mother_id' => 2, 'father_id' => 3)); $Person->save($data); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id))); $this->assertTrue(is_array($result)); $this->assertEqual($result['Aro']['parent_id'], 5); $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8)); $this->assertEqual(count($node), 2); $this->assertEqual($node[0]['Aro']['parent_id'], 5); $this->assertEqual($node[1]['Aro']['parent_id'], null); $aroData = array('Aro' => array('model' => 'AclPerson', 'foreign_key' => 1, 'parent_id' => null)); $this->Aro->create(); $this->Aro->save($aroData); $Person->read(null, 8); $Person->set('mother_id', 1); $Person->save(); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id))); $this->assertTrue(is_array($result)); $this->assertEqual($result['Aro']['parent_id'], 7); $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8)); $this->assertEqual(sizeof($node), 2); $this->assertEqual($node[0]['Aro']['parent_id'], 7); $this->assertEqual($node[1]['Aro']['parent_id'], null); }
/** * Test After Delete * * @return void */ public function testAfterDelete() { $aroData = array('Aro' => array('model' => 'AclPerson', 'foreign_key' => 2, 'parent_id' => null)); $this->Aro->save($aroData); $acoData = array('Aco' => array('model' => 'AclPerson', 'foreign_key' => 2, 'parent_id' => null)); $this->Aco->save($acoData); $Person = new AclPerson(); $data = array('AclPerson' => array('name' => 'Trent', 'mother_id' => 2, 'father_id' => 3)); $Person->save($data); $id = $Person->id; $node = $Person->node(null, 'Aro'); $this->assertEquals(2, count($node)); $this->assertEquals(5, $node[0]['Aro']['parent_id']); $this->assertEquals(null, $node[1]['Aro']['parent_id']); $Person->delete($id); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id))); $this->assertTrue(empty($result)); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2))); $this->assertFalse(empty($result)); $data = array('AclPerson' => array('name' => 'Trent', 'mother_id' => 2, 'father_id' => 3)); $Person->save($data); $id = $Person->id; $Person->delete(2); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id))); $this->assertTrue(empty($result)); $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2))); $this->assertTrue(empty($result)); }
/** * add method * * @return void */ public function add($acoId) { $aco = $this->Acl->Aco->findById($acoId); $this->set('aco', $aco); $acoParent = $this->Acl->Aco->findById($aco['Aco']['parent_id']); $this->set('acoParent', $acoParent); //debug($aco); if ($aco['Aco']['parent_id'] == 1) { $parentAlias = $aco['Aco']['alias']; } else { $parentAlias = $acoParent['Aco']['alias']; } if ($this->request->is('post')) { $this->Page->create(); if ($this->Page->save($this->request->data)) { //Edita o Model e o foreign_key do aco corresponente ao seu Page. $aco = new Aco(); $aco->create(); $aco_options = array('id' => $acoId, 'parent_alias' => $parentAlias, 'model' => 'Page', 'foreign_key' => $this->Page->id); $aco->save($aco_options); $this->Session->setFlash('Acesso controlado salvo com sucesso!', 'alert_success'); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash('O Acesso controlado não pode ser salvo. Tente novamente!', 'alert_error'); } } }
} } else { if ($blogVisibility == 0) { requireOwnership(); } else { if ($blogVisibility == 1) { requireMembership(); } } } } if (in_array($context->getProperty('uri.interfaceType'), array('owner', 'reader'))) { requireOwnership(); // Check access control list if (!empty($_SESSION['acl'])) { $requiredPriv = Aco::getRequiredPrivFromUrl($context->getProperty('suri.directive')); if (!empty($requiredPriv) && !Acl::check($requiredPriv)) { if (in_array('group.administrators', $requiredPriv)) { header("location:" . $context->getProperty('uri.blog') . "/owner/center/dashboard"); exit; } else { header("location:" . $context->getProperty('uri.blog') . "/owner/entry"); exit; } } } } /** INITIALIZE : Cookie prefix * ----------------------------------- * Determines cookie prefix. */
function edit($id = null) { // looking for the current related acos App::import('Model', 'MenuAco'); App::import('Model', 'Aco'); $menuaco = new MenuAco(); $aco = new Aco(); $related = $menuaco->find('all', array('conditions' => array('menu_id' => $id))); foreach ($related as $i => $r) { $parents = $aco->getpath($r['Aco']['id']); $related[$i]['RelatedAco']['url'] = ''; $related[$i]['option'] = 1; // deletes it by default foreach ($parents as $p) { $related[$i]['RelatedAco']['url'] .= $p['Aco']['alias'] . '/'; } $related[$i]['RelatedAco']['url'] = substr($related[$i]['RelatedAco']['url'], 0, -1); } $this->set('related_acos', $related); if (!$id && empty($this->data)) { $this->Session->setFlash(__('Invalid Menu', true)); $this->redirect(array('action' => 'index')); } if (!empty($this->data)) { if ($this->Menu->save($this->data)) { // saves the related acos foreach ($this->data['Aco'] as $i => $aco) { if ($i > 0) { $newaco = $this->Acl->Aco->node($aco); // search the aco if it already exists $found = false; foreach ($related as $k => $r) { if ($r['Aco']['id'] == $newaco[0]['Aco']['id']) { // if its found marks it to not delete it $related[$k]['option'] = 0; // it does nothing $found = true; } } if (!$found) { $menuaco->create(); if ($menuaco->save(array('MenuAco' => array('menu_id' => $id, 'aco_id' => $newaco[0]['Aco']['id'])))) { } else { $this->Session->setFlash(__('The Menu could not be saved. Please, try again.', true)); } } } } foreach ($related as $k => $r) { if ($related[$k]['option'] == 1) { // it does nothing $menuaco->del($r['MenuAco']['id']); } } $this->Session->setFlash(__('The Menu has been saved', true)); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Menu could not be saved. Please, try again.', true)); } } if (empty($this->data)) { $this->data = $this->Menu->read(null, $id); } $userGroups = $this->Menu->UserGroup->find('list'); $this->set(compact('userGroups')); }
function actionAcl() { $aro = new Aro(); $aco = new Aco(); /*$groups = array ( 0=> array ( 'alias'=>'SuperAdministrador', 'parent_id'=>null, 'model'=>'Group', 'foreign_key'=>'1'), 1=> array ( 'alias'=>'Administradores', 'parent_id'=>null, 'model'=>'Group', 'foreign_key'=>'2') ); foreach ($groups as $data) { $aro->create(); $aro->save($data); } $users = array ( 0=> array ( 'alias'=>'admin1', 'parent_id'=>1, 'model'=>'User', 'foreign_key'=>'1' ), 1=> array ( 'alias'=>'a1', 'parent_id'=>2, 'model'=>'User', 'foreign_key'=>'2' ) ); //Iterar y crear los AROs (como hijos) foreach ($users as $data) { ///Recuerda llamar a create() cuando estés guardando información dentro de bucles... $aro->create(); //Guardar datos $aro->save($data); }*/ $groups = array(0 => array('alias' => 'add', 'parent_id' => '39'), 1 => array('alias' => 'index', 'parent_id' => '39'), 2 => array('alias' => 'cPanelConnect', 'parent_id' => '39'), 3 => array('alias' => 'downloadBackup', 'parent_id' => '39')); foreach ($groups as $data) { $aco->create(); $aco->save($data); } /* $this->Acl->allow('SuperAdministrador', 'controllers'); $this->Acl->deny('Administradores', 'controllers'); $this->Acl->allow('Administradores', 'controllers/Domains'); $this->Acl->allow('Administradores', 'controllers/Emailaccounts'); $this->Acl->allow('Administradores', 'controllers/Ftpaccounts');*/ }
/** * Get an array of access-control links between the given Aro and Aco * * @param mixed $aro * @param mixed $aco * @return array * @access public */ function getAclLink($aro, $aco) { $Aro = new Aro(); $Aco = new Aco(); $Link = new ArosAco(); $obj = array(); $obj['Aro'] = $Aro->find($Aro->_resolveID($aro)); $obj['Aco'] = $Aco->find($Aco->_resolveID($aco)); $obj['Aro'] = $obj['Aro']['Aro']; $obj['Aco'] = $obj['Aco']['Aco']; if ($obj['Aro'] == null || count($obj['Aro']) == 0 || $obj['Aco'] == null || count($obj['Aco']) == 0) { return false; } return array('aro' => $obj['Aro']['id'], 'aco' => $obj['Aco']['id'], 'link' => $Link->findAll(array('ArosAco.aro_id' => $obj['Aro']['id'], 'ArosAco.aco_id' => $obj['Aco']['id']))); }
/** * metodo simple para actualizacion de acos * no incluye plugins ni pages ni herencia de metodos del app_controller */ private function update_acos() { $aco = new Aco(); // consulta los controladores en archivos reales $controladores = $this->ControllerList->get(); ksort($controladores); foreach ($controladores as $i => $l) { $controladores[ucfirst($i)] = $controladores[$i]; unset($controladores[$i]); sort($controladores[ucfirst($i)]); } // consultando lista de acos en bd $acos = $aco->find('threaded', array('recursive' => 0, 'order' => 'parent_id, alias')); $lista_bd = array(); foreach ($acos as $a) { $lista_bd[$a['Aco']['alias']]['option'] = 0; $lista_bd[$a['Aco']['alias']]['id'] = $a['Aco']['id']; foreach ($a['children'] as $b) { $lista_bd[$a['Aco']['alias']]['children'][$b['Aco']['alias']]['option'] = 0; $lista_bd[$a['Aco']['alias']]['children'][$b['Aco']['alias']]['id'] = $b['Aco']['id']; foreach ($b['children'] as $c) { $lista_bd[$a['Aco']['alias']]['children'][$b['Aco']['alias']]['children'][$c['Aco']['alias']]['option'] = 0; $lista_bd[$a['Aco']['alias']]['children'][$b['Aco']['alias']]['children'][$c['Aco']['alias']]['id'] = $c['Aco']['id']; } } } // insertando elementos a la lista $lista_bd['controllers']['option'] = !isset($lista_bd['controllers']) ? 1 : 2; // insertar : no hacer nada foreach ($controladores as $i => $l) { $lista_bd['controllers']['children'][$i]['option'] = !isset($lista_bd['controllers']['children'][$i]) ? 1 : 2; // insertar : no hacer nada if ($lista_bd['controllers']['children'][$i]['option'] == 1) { $aco->create(); $aco->save(array('Aco' => array('parent_id' => $lista_bd['controllers']['id'], 'alias' => $i))); $lista_bd['controllers']['children'][$i]['id'] = $aco->getLastInsertId(); } foreach ($controladores[$i] as $k) { $lista_bd['controllers']['children'][$i]['children'][$k]['option'] = !isset($lista_bd['controllers']['children'][$i]['children'][$k]) ? 1 : 2; // insertar : no hacer nada if ($lista_bd['controllers']['children'][$i]['children'][$k]['option'] == 1) { $aco->create(); $aco->save(array('Aco' => array('parent_id' => $lista_bd['controllers']['children'][$i]['id'], 'alias' => $k))); $lista_bd['controllers']['children'][$i]['children'][$k]['id'] = $aco->getLastInsertId(); } } } // borrando elementos foreach ($lista_bd['controllers']['children'] as $i => $l) { if ($lista_bd['controllers']['children'][$i]['option'] == 0) { $aco->id = $lista_bd['controllers']['children'][$i]['id']; $aco->delete(); } foreach ($lista_bd['controllers']['children'][$i]['children'] as $k => $m) { if ($lista_bd['controllers']['children'][$i]['children'][$k]['option'] == 0) { $aco->id = $lista_bd['controllers']['children'][$i]['children'][$k]['id']; $aco->delete(); } } } }
function beforeFilter() { $this->Auth->loginRedirect = '/'; // for the authorizations $this->Auth->authorize = 'actions'; $this->Auth->actionPath = 'controllers/'; // if the user is not logged and it requests the root '/' url // redirects to '/login' if (!$this->Session->read('Auth.User.id')) { if ($this->params['url']['url'] == '/') { $this->redirect('/login'); } } App::import('Model', 'User'); if ($this->Session->read('Auth.User.id')) { if ($this->params['url']['url'] == '/') { $user = new User(); $user = $user->read(null, $this->Session->read('Auth.User.id')); $this->redirect($user['UserGroup']['home_url']); } } if ($this->layout == 'default' && $this->Session->read('Auth.User.id')) { // creating an array with all the permissions for this user // and in the view we check if it has permissions to show the link // getting all the acos with permissions App::import('Model', 'Aco'); App::import('Model', 'Aro'); App::import('Model', 'ArosAco'); $aco = new Aco(); $aro = new Aro(); $aros_aco = new ArosAco(); $acos = $aco->find('threaded', array('recursive' => 0)); // consulta el id del aro $aro_row = $aro->find('first', array('conditions' => array('model' => 'UserGroup', 'foreign_key' => $this->Session->read('Auth.User.user_group_id')))); // consulta la relacion de aros_acos $aros_acos = $aros_aco->find('all', array('conditions' => array('aro_id' => $aro_row['Aro']['id']))); $this->set('acl_lists', $this->AclGetList->get($this->Session->read('Auth.User.user_group_id'), $acos, $aros_acos)); } // rendering the menu for "default" layout // checking if there is a logged user if ($this->layout == 'default' && $this->Session->read('Auth.User.id')) { // renders the menu according the user group App::import('Model', 'Menu'); $menu = new Menu(); $this->set('layout_menus', $menu->find_for_show($this->Session->read('Auth.User.user_group_id'))); // selecting the current menu $this->set('layout_current', $this->params['url']['url']); //$route = Router::parse('/'.$this->params['url']['url']); //$aco_alias = 'controllers/'.$route['controller'].'/'.$route['action']; //$this->set('layout_acos', $this->Acl->Aco->node('controllers/'.$route['controller'].'/'.$route['action'])); // asks the controller for selecting the correct link in the menu $cont = split('_', $this->params['controller']); $newcont = ''; foreach ($cont as $c) { $newcont .= ucfirst($c); } $current_aco = $this->Acl->Aco->node('controllers/' . $newcont . '/' . $this->params['action']); $this->set('layout_aco_id', $current_aco[0]['Aco']['id']); //$route = Router::parse('/profile'); //$this->set('menu_url', '/'.$this->params['controller'].'/'.$this->params['action']); if ($this->Session->read('Auth.User.type') == 'web') { $user = new User(); $user->id = $this->Session->read('Auth.User.id'); $user->updatecredit_expiration(); $this->set('user_balance', $user->getBalance()); $this->set('user_expirationdate', $user->getExpirationDate()); } } }
function admin_create_basic_acl() { Configure::write('debug', '2'); //turn debugging off; debugging breaks ajax die('not now!'); $aco = new Aco(); $aro = new Aro(); /* $aro->create(); $aro->save(array('alias' => 'users')); $aro->create(); $aro->save(array('alias' => 'admins', 'parent_id' => 1)); $aco->create(); $aco->save(array('alias' => 'admin')); $aco->create(); $aco->save(array('alias' => 'site', 'parent_id' => 1)); */ //$this->Acl->grant(array('alias' => 'users'), array('alias' => 'site')); $rr = $aro->findByAlias('users'); $rc = $aco->findByAlias('site'); print_r($rr); $this->Acl->grant(array('Aro' => array('alias' => 'users')), array('Aco' => array('alias' => 'site')), '*'); //$this->Acl->grant(2, 1); //$this->Acl->grant(array('alias' => 'admins'), array('alias' => 'admin')); $this->set('json', 'a'); }
function initAcl() { $aro = new Aro(); $groupsAll = array(0 => array('alias' => 'all')); //Iterate and create ARO groups foreach ($groupsAll as $data) { //Remember to call create() when saving in loops... $aro->create(); //Save data $aro->save($data); } $aro = new Aro(); // $groups=$this->Group->find('all', array('conditions' => array('Group.id' != 1))); // debug($groups); $aroList = array(0 => array('alias' => 'admin', 'parent_id' => 1), 1 => array('alias' => 'user', 'parent_id' => 1), 2 => array('alias' => 'anonymous', 'parent_id' => 1)); // $i=0; // foreach($groups as $group){ // $aroList[$i++]= // array( // 'alias' => $group['Group']['name'], // 'parent_id' => 1, // ); // } //iterate through groups adding to aro table // $groups = array( // 0 => array( // 'alias' => 'users' // ), // 1 => array( // 'alias' => 'administrators' // ), // ); //Iterate and create ARO groups foreach ($aroList as $data) { //Remember to call create() when saving in loops... $aro->create(); //Save data $aro->save($data); } /* * next we add our existing add users to users group * ! adds all users to user group, you may add some logic to * ! detemrine admins based on role, or edit manually later * * the **whos** */ $aro = new Aro(); //pull users form existing user table $users = $this->User->find('all'); debug($users); $i = 0; foreach ($users as $user) { $id = $user['User']['id']; $groupId = $user['User']['group_id']; $aroList2[$i++] = array('alias' => $id, 'parent_id' => $groupId, 'model' => 'User', 'foreign_key' => $id); } //print to screen to verify layout debug($aroList2); //now save! foreach ($aroList2 as $data) { //Remember to call create() when saving in loops... $aro->create(); //Save data $aro->save($data); } /* * now on to *whats* can they access * * for my layout I have the entire site as a parent, two sub groups that contain all models. * */ $aco = new Aco(); //admin can access whole site $controllers = array(0 => array('alias' => 'vsetko')); //Iterate and create ARO groups foreach ($controllers as $data) { //Remember to call create() when saving in loops... $aco->create(); //Save data $aco->save($data); } $aco = new Aco(); //Here's all of our sub-ACO info in an array we can iterate through $controllers = array(0 => array('alias' => 'uploads', 'parent_id' => 1), 1 => array('alias' => 'posts', 'parent_id' => 1), 2 => array('alias' => 'galleries', 'parent_id' => 1), 3 => array('alias' => 'aclpreps', 'parent_id' => 1), 4 => array('alias' => 'p28n', 'parent_id' => 1), 5 => array('alias' => 'welcome', 'parent_id' => 1), 6 => array('alias' => 'about', 'parent_id' => 1), 7 => array('alias' => 'contact', 'parent_id' => 1), 8 => array('alias' => 'products', 'parent_id' => 1), 9 => array('alias' => 'products_form', 'parent_id' => 1)); //Iterate and create ACO nodes foreach ($controllers as $data) { //Remember to call create() when saving in loops... $aco->create(); //Save data $aco->save($data); } die; exit; $this->redirect(array('controller' => 'aclpreps', 'action' => 'index')); }