Beispiel #1
0
 public function actionInit()
 {
     $authManager = \Yii::$app->authManager;
     // Create roles
     $user = $authManager->createRole('user');
     $moderator = $authManager->createRole('moderator');
     $admin = $authManager->createRole('administrator');
     //$guest->ruleName  = 'guest';
     //$user->ruleName  = 'user';
     //$moderator->ruleName = 'moderator';
     //$admin->ruleName = 'administrator';
     // Add roles in Yii::$app->authManager
     $authManager->add($user);
     $authManager->add($moderator);
     $authManager->add($admin);
     $role = $authManager->getRole(UserType::getUserTypeText(UserType::ADMINISTRATOR));
     $authManager->assign($role, 1);
 }
Beispiel #2
0
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $oldPassword = $model->password;
     $oldUserType = $model->user_type;
     $oldImage = $model->image;
     if ($model->load(Yii::$app->request->post())) {
         $model->updated_at = time();
         if ($oldPassword != $model->password) {
             $model->generatePassword($model->password);
         }
         if (!$model->validate()) {
             throw new Exception($model->errors);
         }
         $image = UploadedFile::getInstance($model, 'image');
         if ($image) {
             /**
              * удаление старой картинки, загрузка новой3
              */
             $oldFileUrl = Yii::getAlias('@frontend') . '/web/image/' . $oldImage;
             if (strlen($oldImage)) {
                 unlink($oldFileUrl);
             }
             $model->image = md5(time()) . '.' . $image->extension;
             $image->saveAs(Yii::getAlias('@frontend') . '/web/image/' . $model->image);
         } else {
             $model->image = $oldImage;
         }
         if ($oldUserType != $model->user_type) {
             AuthAssignment::findOne(['user_id' => $model->id, 'item_name' => \common\constants\UserType::getUserTypeText($oldUserType)])->delete();
             $authManager = Yii::$app->authManager;
             $role = $authManager->getRole(\common\constants\UserType::getUserTypeText($model->user_type));
             $authManager->assign($role, $model->id);
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }