public function safeUp() { $app = Yii::$app; $app->setModule('user', ['class' => 'dektrium\\user\\Module']); if (!isset($app->get('i18n')->translations['user*'])) { $app->get('i18n')->translations['user*'] = ['class' => \yii\i18n\PhpMessageSource::className(), 'basePath' => Yii::getAlias('@dektrium/user/migrations')]; } $controller = Yii::$app->controller; $user = new User(['scenario' => 'register']); echo $controller->ansiFormat("\n\n ==> Create Admin User\n", \yii\helpers\Console::FG_CYAN); do { if ($user->hasErrors()) { $this->showErrors($user); } // get email $email = $controller->prompt($controller->ansiFormat("\tE-mail: ", \yii\helpers\Console::FG_BLUE)); // get username $username = $controller->prompt($controller->ansiFormat("\tUsername: "******"\tPassword: "******"\n"; $affectedRows = Yii::$app->db->createCommand()->insert('{{%user}}', ['username' => (string) $username, 'email' => $email, 'password_hash' => Password::hash($password), 'confirmed_at' => new Expression('UNIX_TIMESTAMP()')])->execute(); } while ($affectedRows < 1); do { // get realname $name = $controller->prompt($controller->ansiFormat("\tFull name: ", \yii\helpers\Console::FG_BLUE)); echo "\n\n"; } while (empty($name)); $userPrimaryKey = User::findOne(['email' => $email])->primaryKey; $this->update('{{%profile}}', ['name' => $name], 'user_id=:user_id', [':user_id' => $userPrimaryKey]); $this->insert('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $userPrimaryKey, 'created_at' => new Expression('UNIX_TIMESTAMP()')]); }
/** * @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 ldap() { $authen = false; $ua = base64_encode($_SERVER['HTTP_USER_AGENT']); $apiKey = Yii::$app->params['apiKey']; $password = base64_encode(md5($this->password)); $url = Yii::$app->params['authUrl'] . "/{$ua}/{$apiKey}/{$password}/{$this->username}/json"; $ldap = json_decode(Curl::getData($url)); if (isset($ldap->success) && $ldap->success) { $user = User::findOne(['username' => $this->username]); if (empty($user) || $user === null) { if ($this->allowAddUser) { $this->addUser($ldap); } } else { User::updateAll(['password_hash' => Password::hash($this->password)], ['username' => $this->username]); } if ($this->db()) { $authen = true; } } return $authen; }
/** @inheritdoc */ public function beforeSave($insert) { if ($insert) { $this->setAttribute('auth_key', \Yii::$app->security->generateRandomString()); if (\Yii::$app instanceof \yii\web\Application) { $this->setAttribute('registration_ip', \Yii::$app->request->userIP); } } if (!empty($this->password)) { $this->setAttribute('password_hash', Password::hash($this->password)); } return parent::beforeSave($insert); }
public function up() { $this->insert('user', ['id' => '1', 'username' => 'admin', 'email' => '*****@*****.**', 'password_hash' => Password::hash('admin'), 'auth_key' => Yii::$app->security->generateRandomString(), 'created_at' => time(), 'updated_at' => time(), 'esActivo' => '1']); $this->insert('profile', ['user_id' => '1']); $this->insert('token', ['user_id' => '1', 'code' => Yii::$app->security->generateRandomString(), 'created_at' => time(), 'type' => '0']); }