/**
  * Attaches or detaches user role/permission.
  * @param string $id permission/role name.
  * @param integer $user_id user id.
  * @param integer $add 1/0 whether to add or to remove user permission.
  * @throws \yii\web\HttpException only_root_remove_denied
  */
 public function actionChange($id, $user_id, $add)
 {
     $authItem = AuthItem::findOne($id);
     if ($add) {
         Yii::$app->authManager->assign($authItem, $user_id);
     } else {
         $rootCount = AuthAssignment::find()->where(['item_name' => $id])->count();
         if ($id == 'root' && !$rootCount < 2) {
             throw new HttpException(403, Yii::t('access', 'only_root_remove_denied'));
         }
         Yii::$app->authManager->revoke($authItem, $user_id);
     }
 }
 public function search($params)
 {
     $query = AuthAssignment::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['user_id' => $this->user_id]);
     if ($this->created_at) {
         $time = strtotime($this->created_at);
         $query->andFilterWhere(['between', 'created_at', $time, $time + 86400]);
     }
     $query->andFilterWhere(['like', 'item_name', $this->item_name]);
     return $dataProvider;
 }
 public static function userRolesRemove($event)
 {
     return AuthAssignment::deleteAll(['user_id' => $event->sender->primaryKey]);
 }
Example #4
0
 /**
  * Gets items attached to current one by AuthItemChild relation.
  * @return array of AuthItems
  */
 public function getUsers()
 {
     if (!($user = AuthAssignment::userInstance())) {
         return [];
     }
     return $this->hasMany($user::className(), ['id' => 'user_id'])->via('authAssignments');
 }