public function testSwitchRole() { \Bazalt\Site\Option::set(\Bazalt\Auth::SPLIT_ROLES_OPTION, false); // create role $role = Role::create(); $role->title = 'Test1'; $role->save(); $this->models[] = $role; // create role $role2 = Role::create(); $role2->title = 'Test2'; $role2->save(); $this->models[] = $role2; $this->user->Roles->add($role, ['site_id' => $this->site->id]); $this->user->Roles->add($role2, ['site_id' => $this->site->id]); // print_r($this->user->getRoles()); $curRole = \Bazalt\Auth::getCurrentRole(); // print_r($curRole); $this->assertEquals($role->id, $curRole->id); $this->assertTrue(\Bazalt\Auth::setCurrentRole($role->id)); $curRole = \Bazalt\Auth::getCurrentRole(); $this->assertEquals($role->id, $curRole->id); $this->assertTrue(\Bazalt\Auth::setCurrentRole($role2->id)); $curRole = \Bazalt\Auth::getCurrentRole(); $this->assertEquals($role2->id, $curRole->id); $this->assertFalse(\Bazalt\Auth::setCurrentRole(9999)); //try to set non exists role $curRole = \Bazalt\Auth::getCurrentRole(); $this->assertEquals($role2->id, $curRole->id); \Bazalt\Site\Option::set(\Bazalt\Auth::SPLIT_ROLES_OPTION, true); }
public function getPermissions($site = null) { $site = $site ? $site : \Bazalt\Site::get(); $splitRoles = \Bazalt\Site\Option::get(\Bazalt\Auth::SPLIT_ROLES_OPTION, true); $ret = []; if ($this->is_god) { $q = ORM::select('Bazalt\\Auth\\Model\\Permission p', 'p.id'); $res = $q->fetchAll(); foreach ($res as $perm) { $ret[] = $perm->id; } } else { if ($splitRoles) { $q = ORM::select('Bazalt\\Auth\\Model\\Permission p', 'p.id')->innerJoin('Bazalt\\Auth\\Model\\RoleRefPermission rp', ['permission_id', 'p.id'])->innerJoin('Bazalt\\Auth\\Model\\RoleRefUser ru', ['role_id', 'rp.role_id'])->where('ru.user_id = ?', $this->id); $res = $q->fetchAll(); foreach ($res as $perm) { $ret[] = $perm->id; } } else { $roles = Role::getGuestRoles(); if (!$this->isGuest()) { $currentRole = \Bazalt\Auth::getCurrentRole(); if ($currentRole) { $roles = [$currentRole]; } } foreach ($roles as $role) { $res = $role->getPermissions(); foreach ($res as $perm) { $ret[$perm->id] = $perm->id; } } } } $ret = array_values($ret); if (!$this->isGuest()) { $ret[] = 'auth.user_logged'; } return $ret; }