Example #1
0
 /**
  * Get all routes available for this user
  *
  * @param int  $userId
  * @param bool $withSubRoutes
  *
  * @return array
  */
 public static function getUserRoutes($userId, $withSubRoutes = true)
 {
     $permissions = array_keys(Permission::getUserPermissions($userId));
     if (!$permissions) {
         return [];
     }
     $auth_item = Yii::$app->getModule('user-management')->auth_item_table;
     $auth_item_child = Yii::$app->getModule('user-management')->auth_item_child_table;
     $routes = (new Query())->select(['name'])->from($auth_item)->innerJoin($auth_item_child, '(' . $auth_item_child . '.child = ' . $auth_item . '.name AND ' . $auth_item . '.type = :type)')->params([':type' => self::TYPE_ROUTE])->where([$auth_item_child . '.parent' => $permissions])->column();
     return $withSubRoutes ? static::withSubRoutes($routes, ArrayHelper::map(Route::find()->asArray()->all(), 'name', 'name')) : $routes;
 }
 /**
  * @param int $id User ID
  *
  * @throws \yii\web\NotFoundHttpException
  * @return string
  */
 public function actionSet($id)
 {
     $user = User::findOne($id);
     if (!$user) {
         throw new NotFoundHttpException('User not found');
     }
     $permissionsByGroup = [];
     $permissions = Permission::find()->andWhere([Yii::$app->getModule('user-management')->auth_item_table . '.name' => array_keys(Permission::getUserPermissions($user->id))])->joinWith('group')->all();
     foreach ($permissions as $permission) {
         $permissionsByGroup[@$permission->group->name][] = $permission;
     }
     return $this->renderIsAjax('set', compact('user', 'permissionsByGroup'));
 }
Example #3
0
 /**
  * Gather all user permissions and roles and store them in the session
  *
  * @param UserIdentity $identity
  */
 public static function updatePermissions($identity)
 {
     $session = Yii::$app->session;
     // Clear data first in case we want to refresh permissions
     $session->remove(self::SESSION_PREFIX_ROLES);
     $session->remove(self::SESSION_PREFIX_PERMISSIONS);
     $session->remove(self::SESSION_PREFIX_ROUTES);
     // Set permissions last mod time
     $session->set(self::SESSION_PREFIX_LAST_UPDATE, filemtime(self::getPermissionsLastModFile()));
     // Save roles, permissions and routes in session
     $session->set(self::SESSION_PREFIX_ROLES, array_keys(Role::getUserRoles($identity->id)));
     $session->set(self::SESSION_PREFIX_PERMISSIONS, array_keys(Permission::getUserPermissions($identity->id)));
     $session->set(self::SESSION_PREFIX_ROUTES, Route::getUserRoutes($identity->id));
 }