コード例 #1
0
ファイル: User.php プロジェクト: bemficagama/supletivo
 public function afterSave($insert, $changedAttributes)
 {
     //$oldCategorias = array("0"=>1);
     $actualAssignments = [];
     if (($actualAssignments = AuthAssignment::find()->andWhere("user_id = {$this->id}")->asArray()->all()) !== null) {
         $actualAssignments = ArrayHelper::getColumn($actualAssignments, 'item_name');
     }
     //verifica se é um array
     $this->assignmentIds = is_array($this->assignmentIds) ? $this->assignmentIds : [$this->assignmentIds];
     $inserirAssignments = array_diff($this->assignmentIds, $actualAssignments);
     if (!empty($inserirAssignments)) {
         //save the relations
         foreach ($inserirAssignments as $id) {
             $r = new AuthAssignment();
             $r->user_id = $this->id;
             $r->item_name = $id;
             $r->save();
         }
     }
     $delAssignments = array_diff($actualAssignments, $this->assignmentIds);
     if (!empty($delAssignments)) {
         foreach ($delAssignments as $remove) {
             $r = AuthAssignment::findOne(['item_name' => $remove, 'user_id' => $this->id]);
             $r->delete();
         }
     }
     parent::afterSave($insert, $changedAttributes);
     //don't forget this
 }
コード例 #2
0
ファイル: UserController.php プロジェクト: cakpep/spk-tht
 public function actionAddData()
 {
     $model = new UserBackend();
     $auth = new AuthAssignment();
     if ($model->load(Yii::$app->request->post())) {
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         $user = new User();
         $user->username = $_POST['UserBackend']['username'];
         $user->email = $_POST['UserBackend']['email'];
         $user->setPassword('123456');
         $user->generateAuthKey();
         $user->status = 10;
         if ($user) {
             $auth = new AuthAssignment();
             $auth->item_name = $_POST['AuthAssignment']['item_name'];
             $auth->user_id = 17;
             $auth->created_at = 1428931496;
             if ($auth->save()) {
                 $res = array('message' => 'Data Berhasil Di Simpan.', 'alert' => 'success', 'proses' => 'save', 'success' => true);
             } else {
                 $res = array('message' => 'Data Gagal Di Simpan.', 'alert' => 'error', 'proses' => 'save', 'success' => false);
             }
         } else {
             $res = array('message' => 'Data Gagal Di Simpan.', 'alert' => 'error', 'proses' => 'save', 'success' => false);
         }
         return $res;
         \Yii::$app->end();
     } else {
         return $this->renderAjax('create', ['model' => $model, 'auth' => $auth]);
     }
 }
コード例 #3
0
ファイル: SignupForm.php プロジェクト: hendrasyp/YII2-Setup
 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
     if ($this->validate()) {
         $user = new User();
         $user->company_id = $this->company_id;
         $user->first_name = $this->first_name;
         $user->last_name = $this->last_name;
         $user->username = $this->username;
         $user->email = $this->email;
         $user->setPassword($this->password);
         $user->generateAuthKey();
         if ($user->save()) {
             $permissionList = $_POST['SignupForm']['permissions'];
             foreach ($permissionList as $value) {
                 $newPermissions = new AuthAssignment();
                 $newPermissions->user_id = $user->id;
                 $newPermissions->item_name = $value;
                 $newPermissions->save();
                 $newPermissions->getErrors();
             }
             return $user;
         }
     }
     return null;
 }
 /**
  * Creates a new AuthAssignment model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new AuthAssignment();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->item_name]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #5
0
 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
     if (!$this->validate()) {
         return null;
     }
     $user = new User();
     $user->username = $this->username;
     $user->email = $this->email;
     $user->setPassword($this->password);
     $user->generateAuthKey();
     return $user->save() ? $user : null;
     //creating permissions
     $permissionList = $_POST['SignupForm']['permissions'];
     foreach ($permissionList as $value) {
         $newPermission = new AuthAssignment();
         $newPermission->user_id = $user->id;
         $newPermission->item_name = $value;
         $newPermission->save();
     }
 }
コード例 #6
0
 public function actionAdd()
 {
     $model = new User();
     if (\Yii::$app->request->isPost) {
         $user = \Yii::$app->request->post('User');
         $model->attributes = \Yii::$app->request->post('User');
         $model->position = \Yii::$app->request->post('type');
         $password = \Yii::$app->request->post('password');
         $model->status = 10;
         if ($password) {
             $model->setPassword($password);
             $model->generateAuthKey();
         }
         if ($model->save()) {
             if (\Yii::$app->request->post('type')) {
                 switch (\Yii::$app->request->post('type')) {
                     case 1:
                         $auth = 'thanagorn.role.admin';
                         break;
                     case 2:
                         $auth = 'thanagorn.role.staff';
                         break;
                 }
                 $modelauth = new AuthAssignment();
                 $modelauth->itemname = $auth;
                 $modelauth->userid = $user['username'];
                 $modelauth->data = 'N;';
                 if ($modelauth->save()) {
                     //AuthController::reassign();
                 } else {
                     Ui::setMessage('ไม่สามารถ กำหนนดสิทธิ์ ได้', 'warning');
                 }
             }
             Ui::setMessage('บันทึกข้อมูลสำเร็จ');
             return $this->redirect(Url::toRoute('user/list'));
         } else {
             Ui::setMessage('การบันทึกข้อมูลผิดพลาด', 'warning');
         }
     }
     echo $this->render('add', ['model' => $model]);
 }
コード例 #7
0
ファイル: User.php プロジェクト: darkffh/yii2-lowbase
 /**
  * @inheritdoc
  * Добавление связи роль=>пользователь в таблицу auth_assignment (RBAC)
  */
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     AuthAssignment::deleteAll(['user_id' => $this->id]);
     /** @var \common\models\AuthItem $role */
     $role = AuthItem::findOne($this->role_id);
     if ($role) {
         $assign = new AuthAssignment();
         $assign->user_id = (string) $this->id;
         $assign->item_name = $role->name;
         $assign->created_at = time();
         $assign->save();
     }
     return true;
 }