/** * Get user privileges * * @param integer $userId * @return array */ public function getUserPrivileges($userId) { $roles = Roles\Table::getInstance()->getUserRolesIdentity($userId); $stack = []; foreach ($roles as $roleId) { $stack = array_merge($stack, $this->getRolePrivileges($roleId)); } // magic array_unique for multi array return array_unique($stack); // follow code is faster, but required record for every user in memcache // in other words, need more memory for decrease CPU load // for update /* $cacheKey = 'privileges:user:'******':', p.privilege) FROM acl_privileges AS p, acl_roles AS r, acl_users_roles AS u2r WHERE p.roleId = r.id AND r.id = u2r.roleId AND u2r.userId = ? ORDER BY module, privilege", array((int) $userId) ); Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY); Cache::addTag($cacheKey, 'privileges'); Cache::addTag($cacheKey, 'user:'.$userId); } return $data; */ }
* @accept HTML * @accept JSON * @privilege Management * * @param int $id * @return bool * @throws Exception */ return function ($id) { /** * @var Controller $this */ $user = Users\Table::findRow($id); if (!$user) { throw new Exception('User ID is incorrect'); } if (Request::isPost()) { $roles = Request::getParam('roles'); // update roles Db::delete('acl_users_roles')->where('userId = ?', $user->id)->execute(); foreach ($roles as $role) { Db::insert('acl_users_roles')->set('userId', $user->id)->set('roleId', $role)->execute(); } // clean cache Cache::delete('user:'******'User roles was updated'); return false; } $this->assign('user', $user); $this->assign('roles', Roles\Table::getInstance()->getRoles()); };
/** * Check user role * * @param integer $roleId * @return boolean */ public function hasRole($roleId) { $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id); return in_array($roleId, $roles); }
namespace Application; use Bluz\Proxy\Cache; use Bluz\Proxy\Db; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; return function ($id) use($view) { /** * @var Bootstrap $this */ $user = Users\Table::findRow($id); var_dump($id); if (!$user) { throw new Exception('User ID is incorrect'); } if (Request::isPost()) { $roles = Request::getParam('roles'); // update roles Db::delete('acl_users_roles')->where('userId = ?', $user->id)->execute(); foreach ($roles as $role) { Db::insert('acl_users_roles')->set('userId', $user->id)->set('roleId', $role)->execute(); } // clean cache Cache::delete('user:'******'User roles was updated'); return false; } /* @var $view \Bluz\View\View */ $view->user = $user; $view->roles = Roles\Table::getInstance()->getRoles(); };
/** * isBasic * * @return boolean */ public function isBasic() { return in_array(strtolower($this->name), Table::getInstance()->getBasicRoles()); }