public function getRoles() { // get roles associated with the logged in user $builder = new \Doctrine\DBAL\Query\QueryBuilder($this->em->getConnection()); $builder->select($this->roleIdFieldName, $this->parentRoleFieldName)->from($this->tableName, $this->tableName); $result = $builder->execute(); $roles = array(); foreach ($result as $row) { $roles[] = new Role($row[$this->roleIdFieldName], $row[$this->parentRoleFieldName]); } return $roles; }
/** * Load permissions into roles. * * @param Event $e */ public function loadPermissions(Event $e) { $rbac = $e->getRbac(); $builder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); $options = $this->options; $builder->select("\n p.{$options->getPermissionNameColumn()} AS permission,\n r.{$options->getRoleNameColumn()} AS role\n ")->from($options->getPermissionTable(), 'p')->leftJoin('p', $options->getRoleJoinTable(), 'rp', "rp.{$options->getPermissionJoinColumn()} = p.{$options->getPermissionIdColumn()}")->leftJoin('p', $options->getRoleTable(), 'r', "rp.{$options->getRoleJoinColumn()} = r.{$options->getRoleIdColumn()}"); foreach ($builder->execute() as $row) { if ($rbac->hasRole($row['role'])) { $rbac->getRole($row['role'])->addPermission($row['permission']); } } }
/** * Load roles at RBAC creation. * * @param Event $e * @return array */ public function loadRoles(Event $e) { $builder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); $options = $this->options; $builder->select("role.{$options->getNameColumn()} AS name, parent.{$options->getNameColumn()} AS parent")->from($options->getTable(), 'role')->leftJoin('role', $options->getTable(), 'parent', "role.{$options->getJoinColumn()} = parent.{$options->getIdColumn()}"); $result = $builder->execute(); $roles = array(); foreach ($result as $row) { $parentName = isset($row['parent']) ? $row['parent'] : 0; unset($row['parent']); $roles[$parentName][] = $row['name']; } $this->recursiveRoles($e->getRbac(), $roles); }
public function getIdentityRoles() { $authService = $this->userService->getAuthService(); if (!$authService->hasIdentity()) { // get default/guest role return $this->getDefaultRole(); } else { // get roles associated with the logged in user $builder = new \Doctrine\DBAL\Query\QueryBuilder($this->em->getConnection()); $builder->select("linker.role_id")->from($this->tableName, 'linker')->where('linker.user_id = :user_id')->setParameter('user_id', $authService->getIdentity()->getId()); $result = $builder->execute(); $roles = array(); foreach ($result as $row) { $roles[] = $row['role_id']; } return $roles; } }
public function getRoles() { // get roles associated with the logged in user $builder = new \Doctrine\DBAL\Query\QueryBuilder($this->em->getConnection()); $builder->select($this->roleIdFieldName, $this->parentRoleFieldName)->from($this->tableName, $this->tableName); $rowset = $builder->execute(); $roles = array(); // Pass One: Build each object foreach ($rowset as $row) { $roleId = $row[$this->roleIdFieldName]; $roles[$roleId] = new Role($roleId, $row[$this->parentRoleFieldName]); } // Pass Two: Re-inject parent objects to preserve hierarchy foreach ($roles as $roleId => $roleObj) { $parentRoleObj = $roleObj->getParent(); if ($parentRoleObj && $parentRoleObj->getRoleId()) { $roleObj->setParent($roles[$parentRoleObj->getRoleId()]); } } return array_values($roles); }