Пример #1
0
 public function actionSignupByAdmin()
 {
     $model = new SignupForm();
     $result = [];
     $auth = Yii::$app->authManager;
     if ($model->load(Yii::$app->request->post())) {
         if ($user = $model->signup()) {
             $result['user_id'] = $user->id;
             $role = $auth->getRole(Yii::$app->request->post('role'));
             $auth->assign($role, $user->id);
             $profile = new UserProfileCrud();
             $profile->user_id = $user->id;
             $profile->save();
             $result['success'] = true;
         } else {
             $result['error'] = $model->getErrors();
         }
     }
     echo Json::encode($result);
     Yii::$app->end();
 }
 public function up()
 {
     $user = new User();
     $user->username = Yii::$app->user->superadmin;
     $user->email = '';
     $user->setPassword('123456');
     $user->generateAuthKey();
     if ($user->save()) {
         $auth = Yii::$app->authManager;
         $role = $auth->createRole('admin');
         $auth->add($role);
         $auth->assign($role, $user->id);
         $profile = new UserProfileCrud();
         $profile->user_id = $user->id;
         $profile->firstname = 'admin';
         $profile->lastname = 'admin';
         if (!$profile->save()) {
             foreach ($profile->getErrors() as $k => $v) {
                 echo "[{$k}] {$v['0']}";
             }
         }
     }
 }