public function signIn() { $attributes = $this->getUserAttributes(); $password = Yii::$app->security->generateRandomString(6); $user = new User(['password' => $password]); if ($attributes['emails']) { $email = current($attributes['emails']); $user->email = $email['value']; } if ($attributes['name']) { $user->first_name = isset($attributes['name']['givenName']) ? $attributes['name']['givenName'] : ''; $user->last_name = isset($attributes['name']['familyName']) ? $attributes['name']['familyName'] : ''; } $user->generateAuthKey(); $user->generatePasswordResetToken(); $transaction = $user->getDb()->beginTransaction(); if ($user->save()) { // create empty profile $profile = new UserProfile(['user_id' => $user->getId()]); $profile->save(); if (isset($attributes['image'])) { if ($attributes['image']['url']) { // upload facebook images $prepareUrl = substr($attributes['image']['url'], 0, strpos($attributes['image']['url'], '?')); $fname = basename($prepareUrl); $ch = curl_init($attributes['image']['url']); $fp = fopen(Yii::getAlias('@uploads/users/' . $fname), 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); $user->avatar = $fname; } } $user->update(false); // assign role default ROLE_USER $authManager = Yii::$app->authManager; $authManager->assign($authManager->getRole(User::ROLE_USER), $user->getId()); $auth = new UserAuth(['user_id' => $user->id, 'source' => $this->getId(), 'source_id' => (string) $attributes['id']]); if ($auth->save()) { $transaction->commit(); // auto login Yii::$app->user->login($user); return true; } else { Yii::$app->getSession()->setFlash('error', 'Auth client "' . $this->getTitle() . '" not connected'); } } else { Yii::$app->getSession()->setFlash('error', 'User "' . $attributes['login'] . '" not register'); } return false; }
public function beforeSave($insert) { $this->updatedon = date('Y-m-d H:i:s', time()); if (UserProfile::find()->count() < 30) { return true; } return false; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = UserProfile::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'fullname', $this->fullname])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'address', $this->address])->andFilterWhere(['like', 'country', $this->country])->andFilterWhere(['like', 'city', $this->city])->andFilterWhere(['like', 'state', $this->state])->andFilterWhere(['like', 'zip', $this->zip])->andFilterWhere(['like', 'website', $this->website])->andFilterWhere(['like', 'skype', $this->skype])->andFilterWhere(['like', 'image', $this->image]); return $dataProvider; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); // start transaction $transaction = $user->getDb()->beginTransaction(); if ($user->save()) { // create empty profile $profile = new UserProfile(['user_id' => $user->getId()]); $profile->save(); // assign disable user role ROLE_DISABLE $authManager = Yii::$app->authManager; $authManager->assign($authManager->getRole(User::ROLE_DISABLE), $user->getId()); // end transaction $transaction->commit(); return $user; } } return null; }
public function actionPosOwner($id) { $model = UserProfile::findOne(['user_id' => $id]); return $this->render('viewPosOwner', ['model' => $model]); }
public function down() { $this->dropTable(UserProfile::tableName()); }
public function getProfile() { return $this->hasOne(UserProfile::className(), ['user_id' => 'id'])->inverseOf('user'); }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post())) { $model->setPassword($model->password); $model->generateAuthKey(); $model->photoFile = UploadedFile::getInstance($model, 'photoFile'); if ($model->photoFile && !$model->upload()) { Yii::$app->session->setFlash('error', Yii::t('app', 'Image not upload')); return $this->render('create', ['model' => $model]); } if ($model->save()) { // change role if (!$model->role) { $model->role = User::ROLE_USER; } User::changeRole($model->role, $model->getId()); // create profile $profile = new UserProfile(); $profile->user_id = $model->id; $profile->save(false); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Deletes an existing UserProfile model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); if (($modelProfile = UserProfile::findOne(['user_id' => $id])) !== null) { $modelProfile->delete(); } return $this->redirect(['index']); }