Example #1
0
 /** Create user */
 public function actionCreate()
 {
     $user = new User(['scenario' => 'admin-create']);
     $statusArray = User::getStatusArray();
     $roles = ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description');
     if ($user->load(Yii::$app->request->post())) {
         if ($user->validate()) {
             if ($user->save(false)) {
                 foreach (Yii::$app->request->post('roles') as $role) {
                     $new_role = Yii::$app->authManager->getRole($role);
                     Yii::$app->authManager->assign($new_role, $user->getId());
                 }
                 Yii::$app->session->setFlash('success', Yii::t('userscube', 'USER_CREATE_SUCCESS'));
                 return $this->redirect(['update', 'id' => $user->id]);
             } else {
                 Yii::$app->session->setFlash('danger', Yii::t('userscube', 'USER_CREATE_FAIL'));
                 return $this->refresh();
             }
         } elseif (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($user);
         }
     }
     return $this->render('create', ['user' => $user, 'roles' => $roles, 'statusArray' => $statusArray]);
 }
 /** Sign Up page. (Registration) */
 public function actionSignup()
 {
     $this->layout = '@vendor/cubiclab/admin-cube/views/layouts/signin';
     $user = new User(['scenario' => 'register']);
     // if register action is triggered
     if ($user->load(Yii::$app->request->post())) {
         if ($user->validate()) {
             if ($user->save(false)) {
                 Yii::$app->user->login($user);
                 Yii::$app->session->setFlash('success', 'Register success');
                 return $this->goHome();
             } else {
                 Yii::$app->session->setFlash('danger', 'Register error');
                 return $this->refresh();
             }
         } elseif (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($user);
         }
     }
     // render view
     return $this->render('signup', ['user' => $user]);
 }