public function action_view($user_id = null) { if (empty($user_id)) { $user_id = $this->user->id; } try { $this->include_client_scripts('jquery_forms'); $this->template->content = \View::forge('member/account'); $this->template->content->user = $this->user->id == $user_id ? $this->user : \Warden\Model_User::authenticate($user_id, true); if (!$this->template->content->user) { \Session::set_flash('error', "User '{$user_id}' wasn't found in our system."); \Response::redirect('/welcome/404'); } $this->template->content->editable = $this->user->id == $user_id && \Access::can('edit_own_account', $this->user) || \Access::can('edit_any_account', $this->user); $this->template->content->can_unlock = $this->template->content->user->is_access_locked() && \Access::can('unlock_any_user', $this->user); $this->template->content->title = $this->user->id == $user_id ? 'My Account' : $this->template->content->user->username; $this->template->title = $this->template->content->title; if (\Access::can('assign_roles', $this->user)) { $result = \Warden\Model_Role::find('all'); $this->template->content->roles = array(); foreach ($result as $row) { $this->template->content->roles[$row['id']] = $row['name']; } } } catch (Exception $e) { \Session::set_flash('error', $e->getMessage()); \Response::redirect('/welcome/404'); } }
public static function assign_role($role_id, $user = null) { if (empty($user)) { $user = \Warden::current_user(); } else { if (!is_object($user)) { $user = \Warden\Model_User::find($user); } } if (!$user || !is_object($user)) { throw new \Exception("Cannot assign role to a user that doesn't exist"); } try { $role = \Warden\Model_Role::find($role_id); $user->roles[$role_id] = $role; $user->save(); } catch (\Exception $e) { throw $e; } }