public function testRoleContextBucket() { $acl = $this->getAcl(); $rb = new RoleBucket($acl); $rb->add('guest'); $rb->add('super_administrator'); $acl->switchRoleContext($rb); $context = $acl->getContext(); $this->assertEquals($context->getRoles()->toArray(), ['guest', 'super_administrator']); }
/** * @inheritdoc */ public function getRoles() { if (!isset($this->roles)) { $this->roles = parent::getRoles(); $roles = $this->getRoleQuery()->pluck('role_id'); foreach ($roles as $role) { if ($role = app(RoleProvider::class)->getRoleById($role)) { $this->roles->add($role['system_id']); } } } return clone $this->roles; }
/** * Switch to a explicit role (used in testing) * * @param array|string|RoleBucket $roles * @return AclContextContract */ public function switchRoleContext($roles) { if (is_string($roles)) { $roles = [$roles]; } if (is_array($roles)) { $roleBucket = new RoleBucket($this); foreach ($roles as $role) { $roleBucket->add($role); } } else { $roleBucket = $roles; } $context = new RoleSetContext($roleBucket); $this->registerContext($context); $this->currentContext = $context->getId(); return $context; }
/** * @inheritdoc */ protected function id() { return sha1(implode($this->roles->toArray())); }
private function discoverSelf(RoleBucket $roleBucket) { if ($this->isSelf()) { $roleBucket->add('self'); } }