/**
  * Creates a new SocialServiceManager model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new SocialServiceManager();
     if ($model->load(Yii::$app->request->post())) {
         $person = new Person();
         $person->name = $model->name;
         $person->lastname = $model->last_name;
         $person->phone = $model->phone;
         $person->save(false);
         $user = new User();
         $user->username = $model->username;
         $user->password = $model->password;
         $user->email = $model->email;
         $user->person_id = $person->id;
         $user->scenario = 'register';
         if ($user->validate(['username', 'password'])) {
             $user->register();
             $model->user_id = $user->id;
             $model->save(false);
             //assign the role to the user
             $authManager = Yii::$app->getAuthManager();
             $socialServiceMRole = $authManager->getRole('socialServiceManager');
             $authManager->assign($socialServiceMRole, $user->id);
             //set the success message
             Yii::$app->getSession()->setFlash('success', 'Usuario creado con éxito');
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             $model->addErrors($user->errors);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
 public function loadAttributes(User $user)
 {
     $user->setAttributes($this->attributes);
     $profile = \Yii::createObject(Profile::className());
     $profile->setAttributes(['name' => $this->name]);
     $user->setProfile($profile);
 }
 /**
  * @inheritdoc
  */
 protected function loadAttributes(\dektrium\user\models\User $user)
 {
     $user->setAttributes(['email' => $this->email, 'username' => $this->username, 'password' => $this->password]);
     $profile = \Yii::createObject(Profile::className());
     $profile->setAttributes(['name' => ucwords(strtolower($this->firstname)) . " " . ucwords(strtolower($this->lastname)), 'firstname' => ucwords(strtolower($this->firstname)), 'lastname' => ucwords(strtolower($this->lastname)), 'birthday' => $this->birthday, 'terms' => $this->terms]);
     $user->setProfile($profile);
 }
Esempio n. 4
0
 /**
  * Registers a new user account.
  * @return bool
  */
 public function register()
 {
     if (!$this->validate()) {
         return false;
     }
     $this->user->setAttributes(['email' => $this->email, 'username' => $this->username, 'password' => $this->password]);
     return $this->user->register();
 }
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'exist', 'targetClass' => $this->module->manager->userClass, 'message' => \Yii::t('user', 'There is no user with such email.')], ['email', function ($attribute) {
         $this->_user = $this->module->manager->findUserByEmail($this->email);
         if ($this->_user !== null && $this->getModule()->enableConfirmation && !$this->_user->getIsConfirmed()) {
             $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address'));
         }
     }]];
 }
Esempio n. 6
0
 /** @inheritdoc */
 public function rules()
 {
     return ['emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => \Yii::t('user', 'There is no user with this email address')], 'emailUnconfirmed' => ['email', function ($attribute) {
         $this->user = $this->finder->findUserByEmail($this->email);
         if ($this->user !== null && $this->module->enableConfirmation && !$this->user->getIsConfirmed()) {
             $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address'));
         }
     }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]];
 }
Esempio n. 7
0
 public function rules()
 {
     $rules = parent::rules();
     // add some rules
     $rules['vmlistLength'] = ['vmlist', 'string', 'max' => 255];
     return $rules;
 }
Esempio n. 8
0
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if ($insert) {
         UcenterUtil::register($this->username, $this->password, $this->email);
     }
 }
 public function attributeLabels()
 {
     $labels = parent::attributeLabels();
     $labels['direccion'] = Yii::t('core', 'Adress');
     $labels['esActivo'] = Yii::t('core', 'Active?');
     return $labels;
 }
Esempio n. 10
0
 public function actionConnect($account_id)
 {
     //var_dump("connect");die;
     $account = $this->finder->findAccountById($account_id);
     if ($account === null || $account->getIsConnected()) {
         throw new NotFoundHttpException();
     }
     /** @var User $user */
     $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'connect']);
     if (\Yii::$app->request->get("provider") == 'kd') {
         $data = array();
         $data['User'] = array();
         $data['User']['username'] = \Yii::$app->request->get("username");
         $data['User']['email'] = \Yii::$app->request->get("email");
     } else {
         $data = \Yii::$app->request->post();
     }
     if ($user->load($data) && $user->create()) {
         $account->user_id = $user->id;
         $account->save(false);
         \Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account]);
 }
 public function safeDown()
 {
     $controller = Yii::$app->controller;
     $model = \Yii::createObject(LoginForm::className());
     do {
         if ($model->hasErrors()) {
             $this->showErrors($model);
         }
         // get username
         $username = $controller->prompt($controller->ansiFormat("\tUsername: "******"\tPassword: "******"\n";
         $model->login = $username;
         $model->password = $password;
     } while (!$model->validate());
     $user = User::findOne(['username' => $username]);
     if (empty($user)) {
         throw new \yii\console\Exception("Unable to find user {$username}");
     }
     $this->delete('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $user->primaryKey]);
     $user->delete();
 }
Esempio n. 12
0
 private static function checkUserSetup()
 {
     if (UserModel::find()->where('id != 1')->count() == 0) {
         $link = Html::a('user module', ['/user/admin/create']);
         \Yii::$app->session->addFlash('warning', "There is no additional user registered, visit {$link} to create an editor.");
     }
 }
Esempio n. 13
0
 /**
  * Resets user's password.
  *
  * @return bool
  */
 public function resetPassword()
 {
     if ($this->validate()) {
         $this->_user->resetPassword($this->password);
         return true;
     }
     return false;
 }
 protected function findModel($id)
 {
     if (($model = User::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Esempio n. 15
0
 public function rules()
 {
     $rules = parent::rules();
     // add some rules
     $rules['fieldRequired'] = ['role', 'required'];
     $rules['fieldLength'] = ['role', 'string', 'max' => 10];
     return $rules;
 }
Esempio n. 16
0
 /** @inheritdoc */
 public function rules()
 {
     return ['requiredFields' => [['login', 'password'], 'required'], 'loginTrim' => ['login', 'trim'], 'passwordValidate' => ['password', function ($attribute) {
         if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) {
             $this->addError($attribute, Yii::t('user', 'Invalid login or password'));
         }
     }], 'confirmationValidate' => ['login', function ($attribute) {
         if ($this->user !== null) {
             $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin;
             if ($confirmationRequired && !$this->user->getIsConfirmed()) {
                 $this->addError($attribute, Yii::t('user', 'You need to confirm your email address'));
             }
             if ($this->user->getIsBlocked()) {
                 $this->addError($attribute, Yii::t('user', 'Your account has been blocked'));
             }
         }
     }], 'rememberMe' => ['rememberMe', 'boolean']];
 }
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     if ($insert) {
         $this->setAttribute('auth_key', Yii::$app->getSecurity()->generateRandomKey());
     }
     if (!empty($this->password)) {
         $this->setAttribute('password_hash', Password::hash($this->password));
     }
     return parent::beforeSave($insert);
 }
 public function actionFollowings($id)
 {
     $followings = FollowerUsertoUser::find()->where(['follower_user_id' => $id])->all();
     $following_users_ids = ArrayHelper::getColumn($followings, 'followed_user_id');
     $query = User::find()->where(['id' => $following_users_ids]);
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count()]);
     $models = $query->offset($pages->offset)->limit($pages->limit)->all();
     return $this->render('index', ['models' => $models, 'caller' => $id, 'callerType' => 'Followings']);
 }
Esempio n. 19
0
 /**
  * Validates form and logs the user in.
  *
  * @return bool whether the user is logged in successfully
  */
 public function login()
 {
     if ($this->validate()) {
         if (Yii::$app->getUser()->login($this->user, $this->rememberMe ? $this->module->rememberFor : 0)) {
             UserLog::log("login-success");
             $this->user->last_login = gmdate("Y-m-d H:i:s");
             $this->user->save();
             return true;
         } else {
             return false;
         }
     } else {
         $message = '';
         foreach ($this->errors['login'] as $error) {
             $message .= $error . "\n";
         }
         UserLog::log("login-failure", $message);
         return false;
     }
 }
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'index' page.
  *
  * @return mixed
  */
 public function actionCreate()
 {
     /** @var User $user */
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'create']);
     $this->performAjaxValidation($user);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created'));
         return $this->redirect(['update', 'id' => $user->id]);
     }
     return $this->render('create', ['user' => $user]);
 }
Esempio n. 21
0
 public function testRegister()
 {
     $this->specify('user should be registered', function () {
         $user = new User(['scenario' => 'register']);
         $user->username = '******';
         $user->email = '*****@*****.**';
         $user->password = '******';
         verify($user->register())->true();
         verify($user->username)->equals('tester');
         verify($user->email)->equals('*****@*****.**');
         verify(Yii::$app->getSecurity()->validatePassword('tester', $user->password_hash))->true();
     });
     $this->specify('profile should be created after registration', function () {
         $user = new User(['scenario' => 'register']);
         $user->username = '******';
         $user->email = '*****@*****.**';
         $user->password = '******';
         verify($user->register())->true();
         verify($user->profile->gravatar_email)->equals('*****@*****.**');
     });
 }
Esempio n. 22
0
 public function rules()
 {
     $rules = parent::rules();
     // add some rules
     $rules['firstNameRequired'] = ['first_name', 'required'];
     $rules['firstNameLength'] = ['first_name', 'string', 'max' => 255];
     $rules['lastNameRequired'] = ['last_name', 'required'];
     $rules['lastNameLength'] = ['last_name', 'string', 'max' => 255];
     $rules['countryRequired'] = ['country', 'required'];
     $rules['countryLength'] = ['country', 'string', 'max' => 2];
     return $rules;
 }
Esempio n. 23
0
 public function actionAssign($role, $userId)
 {
     $user = User::findOne($userId);
     if (!$user) {
         throw new InvalidParamException('There is no such user.');
     }
     $auth = Yii::$app->authManager;
     $role = $auth->getRole($role);
     if (!$role) {
         throw new InvalidParamException('There is no such role.');
     }
     $auth->assign($role, $userId);
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Ruta::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $user = User::findOne(['username' => $this->id_usuario]);
     $query->andFilterWhere(['id' => $this->id]);
     if ($this->id_usuario != null) {
         $query->andFilterWhere(['id_usuario' => $user != null ? $user->id : 0]);
     }
     if (strtolower($this->esActivo) == strtolower(Yii::t('core', 'Yes'))) {
         $query->andFilterWhere(['esActivo' => $this->esActivo == 0]);
     } else {
         if (strtolower($this->esActivo) == strtolower(Yii::t('core', 'No'))) {
             $query->andFilterWhere(['esActivo' => $this->esActivo == 1]);
         }
     }
     if (strtolower($this->dia) == strtolower(Yii::t('core', 'Monday'))) {
         $query->andFilterWhere(['dia' => 1]);
     } else {
         if (strtolower($this->dia) == strtolower(Yii::t('core', 'Tuesday'))) {
             $query->andFilterWhere(['dia' => 2]);
         } else {
             if (strtolower($this->dia) == strtolower(Yii::t('core', 'Wednesday'))) {
                 $query->andFilterWhere(['dia' => 3]);
             } else {
                 if (strtolower($this->dia) == strtolower(Yii::t('core', 'Thursday'))) {
                     $query->andFilterWhere(['dia' => 4]);
                 } else {
                     if (strtolower($this->dia) == strtolower(Yii::t('core', 'Friday'))) {
                         $query->andFilterWhere(['dia' => 5]);
                     } else {
                         if (strtolower($this->dia) == strtolower(Yii::t('core', 'Saturday'))) {
                             $query->andFilterWhere(['dia' => 6]);
                         } else {
                             if (strtolower($this->dia) == strtolower(Yii::t('core', 'Sunday'))) {
                                 $query->andFilterWhere(['dia' => 7]);
                             }
                         }
                     }
                 }
             }
         }
     }
     return $dataProvider;
 }
Esempio n. 25
0
 /**
  * This command creates new user account. If password is not set, this command will generate new 8-char password.
  * After saving user to database, this command uses mailer component to send credentials (username and password) to
  * user via email.
  *
  * @param string      $email    Email address
  * @param string      $username Username
  * @param null|string $password Password (if null it will be generated automatically)
  */
 public function actionIndex($email, $username, $password = null)
 {
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]);
     if ($user->create()) {
         $this->stdout(Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN);
     } else {
         $this->stdout(Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED);
         foreach ($user->errors as $errors) {
             foreach ($errors as $error) {
                 $this->stdout(' - ' . $error . "\n", Console::FG_RED);
             }
         }
     }
 }
Esempio n. 26
0
 /**
  * Tests login method.
  */
 public function testLogin()
 {
     $user = \Yii::createObject(User::className());
     test::double(Finder::className(), ['findUserByUsernameOrEmail' => $user]);
     $form = Yii::createObject(LoginForm::className());
     $form->beforeValidate();
     test::double($form, ['validate' => false]);
     verify($form->login())->false();
     test::double($form, ['validate' => true]);
     test::double(\yii\web\User::className(), ['login' => false]);
     verify($form->login())->false();
     test::double(\yii\web\User::className(), ['login' => true]);
     verify($form->login())->true();
 }
 /**
  * Displays page where user can create new account that will be connected to social account.
  * @param  integer $account_id
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionConnect($account_id)
 {
     $account = $this->finder->findAccountById($account_id);
     if ($account === null || $account->getIsConnected()) {
         throw new NotFoundHttpException();
     }
     /** @var User $user */
     $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'connect']);
     if ($user->load(\Yii::$app->request->post()) && $user->create()) {
         $account->link('user', $user);
         \Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account]);
 }
 public function up()
 {
     if (isset($_SERVER['TRAVIS']) || isset($_SERVER['PWD']) && strpos($_SERVER['PWD'], '/tests/')) {
         return null;
     }
     $userName = '******';
     $tableName = \dektrium\user\models\User::tableName();
     $query = 'SELECT COUNT(*) FROM ' . $tableName . ' WHERE `username`=:username';
     $count = \Yii::$app->db->createCommand($query, [':username' => $userName])->queryScalar();
     if ($count > 0) {
         return null;
     }
     $user = \Yii::createObject(['class' => \dektrium\user\models\User::className(), 'scenario' => 'create', 'username' => $userName, 'password' => $userName, 'email' => $userName . '@yii2enterprise.dev']);
     return $user->create();
 }
 /**
  * Displays page where user can create new account that will be connected to social account.
  *
  * @param string $code
  *
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionConnect($code)
 {
     $account = $this->finder->findAccount()->byCode($code)->one();
     if ($account === null || $account->getIsConnected()) {
         throw new NotFoundHttpException();
     }
     /** @var User $user */
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         $account->connect($user);
         Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account]);
 }
Esempio n. 30
0
 /**
  * the register customer will be added  'customer' role
  */
 public static function frontendRegister()
 {
     Event::on(User::className(), User::USER_REGISTER_DONE, function ($event) {
         /** @var \yii\base\ModelEvent $event */
         $user = $event->sender;
         $auth = new DbManager();
         $auth->init();
         $role = $auth->getRole('Customer');
         if (!$role) {
             $role = $auth->createRole('Customer');
             $auth->add($role);
         }
         $auth->assign($role, $user->id);
     });
 }