11 | * @since 1.0.0
12 | *
13 | * @property integer $id
14 | * @property string $login
15 | * @property string $username
16 | * @property string $password_hash
17 | * @property string $auth_key
18 | * @property integer $administrator
19 | * @property integer $creator
20 | * @property string $creator_ip
21 | * @property string $confirm_token
22 | * @property string $recovery_token
23 | * @property integer $blocked_at
24 | * @property integer $confirmed_at
25 | * @property integer $created_at
26 | * @property integer $updated_at
27 | */
28 | class UserAccounts extends ActiveRecord implements IdentityInterface {
29 |
30 | /**
31 | * @var User Accounts Event
32 | */
33 | const BEFORE_CREATE = 'beforeCreate';
34 |
35 | /**
36 | * @var User Accounts Event
37 | */
38 | const AFTER_CREATE = 'afterCreate';
39 |
40 | /**
41 | * @var User Accounts Event
42 | */
43 | const BEFORE_CONSOLE_CREATE = 'beforeConsoleCreate';
44 |
45 | /**
46 | * @var User Accounts Event
47 | */
48 | const AFTER_CONSOLE_CREATE = 'afterConsoleCreate';
49 |
50 | /**
51 | * @var User Accounts Event
52 | */
53 | const BEFORE_REGISTER = 'beforeRegister';
54 |
55 | /**
56 | * @var User Accounts Event
57 | */
58 | const AFTER_REGISTER = 'afterRegister';
59 |
60 | /**
61 | * @var User Accounts Event
62 | */
63 | const BEFORE_CHANGE_PASSWORD = 'beforeChangePassword';
64 |
65 | /**
66 | * @var User Accounts Event
67 | */
68 | const AFTER_CHANGE_PASSWORD = 'afterChangePassword';
69 |
70 | /**
71 | * @var Use for creator field when user created by console application
72 | */
73 | const CREATOR_BY_CONSOLE = -2;
74 |
75 | /**
76 | * @var Use for creator field when user registed by yourself
77 | */
78 | const CREATOR_BY_REGISTER = -1;
79 |
80 | /**
81 | *
82 | * @var string User's plain password
83 | */
84 | public $password;
85 |
86 | /**
87 | *
88 | * @var string User comfirm password(Need for creat,register,change password)
89 | */
90 | public $confirm_password;
91 |
92 |
93 | /**
94 | *
95 | * @var string Old password use for change password
96 | */
97 | public $old_password;
98 |
99 | /**
100 | *
101 | * @var string New password use for change password
102 | */
103 | public $new_password;
104 |
105 | /**
106 | * Returns the validation rules for attributes.
107 | * @return array validation rules
108 | * @see http://www.yiiframework.com/doc-2.0/yii-base-model.html#rules()-detail
109 | */
110 | public function rules() {
111 | return [
112 | // login rules
113 | 'loginRequired' => ['login', 'required', 'on' => ['register', 'create', 'console-create']],
114 | 'loginLength' => ['login', 'string', 'max' => 255],
115 | 'loginUnique' => ['login', 'unique', 'message' => Yii::t('user', 'This account name has already been taken')],
116 | 'loginTrim' => ['login', 'trim'],
117 | // password rules
118 | 'passwordRequired' => ['password', 'required', 'on' => ['register', 'create', 'console-create']],
119 | 'passwordLength' => ['password', 'string', 'min' => 6],
120 | //confirm password rules
121 | 'confirmPasswordRequired' => ['password', 'required', 'on' => ['register', 'create']],
122 | 'confirmPasswordLength' => ['confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => Yii::t("user", "Comfirm Passwords don't match")],
123 | 'oldPasswordRequired' => ['old_password', 'required', 'on' => ['change_password']],
124 |
125 | ];
126 | }
127 |
128 | /**
129 | * Returns a list of scenarios and the corresponding active attributes.
130 | * @return array a list of scenarios and the corresponding active attributes.
131 | * @see http://www.yiiframework.com/doc-2.0/yii-base-model.html#scenarios()-detail
132 | */
133 | public function scenarios() {
134 | return [
135 | 'create' => ['login', 'password', 'confirm_password'],
136 | 'register' => ['login', 'password', 'confirm_password', 'confirm_token'],
137 | 'console-create' => ['login', 'password'],
138 | 'toggle-block' => ['blocked_at'],
139 | 'block' => ['blocked_at'],
140 | 'unblock' => ['blocked_at'],
141 | 'toggle-administrator' => ['administrator'],
142 | 'update'=> ['password', 'confirm_password'],
143 | 'change_password' => ['password', 'confirm_password','new_password','old_password'],
144 | ];
145 | }
146 |
147 | /**
148 | * Returns the attribute labels.
149 | * @return array Attribute labels (name => label).
150 | * @see http://www.yiiframework.com/doc-2.0/yii-base-model.html#attributeLabels()-detail
151 | */
152 | public function attributeLabels()
153 | {
154 | return [
155 | 'id' => Yii::t('app', 'ID'),
156 | 'login' => Yii::t('app', 'Login'),
157 | 'password_hash' => Yii::t('app', 'Password Hash'),
158 | 'auth_key' => Yii::t('app', 'Auth Key'),
159 | 'administrator' => Yii::t('app', 'Administrator'),
160 | 'creator' => Yii::t('app', 'Creator'),
161 | 'creator_ip' => Yii::t('app', 'Creator Ip'),
162 | 'confirm_token' => Yii::t('app', 'Confirm Token'),
163 | 'recovery_token' => Yii::t('app', 'Recovery Token'),
164 | 'blocked_at' => Yii::t('app', 'Blocked At'),
165 | 'confirmed_at' => Yii::t('app', 'Confirmed At'),
166 | 'created_at' => Yii::t('app', 'Created At'),
167 | 'updated_at' => Yii::t('app', 'Updated At'),
168 | ];
169 | }
170 |
171 | /**
172 | * This method is called at the beginning of inserting or updating a record.
173 | * @param boolean $insert whether this method called while inserting a record.
174 | * If false, it means the method is called while updating a record.
175 | * @return boolean whether the insertion or updating should continue.
176 | * If false, the insertion or updating will be cancelled.
177 | * @see http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#beforeSave()-detail
178 | */
179 | public function beforeSave($insert) {
180 | if ($insert) {
181 | $this->blocked_at = null;
182 | $this->created_at = time();
183 | $this->updated_at = -1;
184 | } else {
185 | $this->updated_at = time();
186 | }
187 |
188 | if ($this->password !== null) {
189 | $this->setPassword($this->password);
190 | }
191 | return true;
192 | }
193 |
194 |
195 | public function consoleCreate(){
196 | $this->trigger(self::BEFORE_CONSOLE_CREATE);
197 |
198 | $this->administrator = true;
199 | $this->creator = self::CREATOR_BY_CONSOLE;
200 | $this->creator_ip = Yii::t('user','Local');
201 | $this->confirmed_at = time();
202 |
203 | if(!$this->save()){
204 | return false;
205 | }
206 | $this->trigger(self::AFTER_CONSOLE_CREATE);
207 | return true;
208 | }
209 |
210 |
211 | /**
212 | * Create user
213 | * @return boolean whether user creat success
214 | */
215 | public function create($creatorUserId){
216 | $this->trigger(self::BEFORE_CREATE);
217 |
218 | $this->creator = $creatorUserId;
219 | $this->administrator = false;
220 | $this->confirmed_at = time();
221 | $this->prepareCreatorIp();
222 |
223 | if(!$this->save()){
224 | return false;
225 | }
226 | $this->trigger(self::AFTER_CREATE);
227 | return true;
228 | }
229 |
230 | /**
231 | * Register user
232 | * @return boolean whether user register success
233 | */
234 | public function register(){
235 | $this->trigger(self::BEFORE_REGISTER);
236 |
237 | $this->administrator = false;
238 | $this->creator = self::CREATOR_BY_REGISTER;
239 | $this->prepareCreatorIp();
240 |
241 | if(!$this->save()){
242 | return false;
243 | }
244 |
245 | $this->trigger(self::AFTER_REGISTER);
246 | return true;
247 | }
248 |
249 | /**
250 | * Register user
251 | * @return boolean whether user register success
252 | */
253 | public function changePassword(){
254 | $this->trigger(self::BEFORE_CHANGE_PASSWORD);
255 | if($this->validatePassword($this->old_password)){
256 | $this->password = $this->new_password;
257 | if(!$this->save()){
258 | return false;
259 | }
260 | }else{
261 | $this->addError('old_password',Yii::t("user","Your current password is not match"));
262 | return false;
263 | }
264 |
265 |
266 | $this->trigger(self::AFTER_CHANGE_PASSWORD);
267 | return true;
268 | }
269 |
270 | /**
271 | * Block user
272 | * @return boolean whether user block success
273 | */
274 | public function block(){
275 | $this->blocked_at = time();
276 | return $this->save();
277 | }
278 |
279 | /**
280 | * Unblock user
281 | * @return boolean whether user unblock success
282 | */
283 | public function unblock(){
284 | $this->blocked_at = null;
285 | return $this->save();
286 | }
287 |
288 | /**
289 | * Toggle block user
290 | * @return boolean whether toogle success
291 | */
292 | public function toggleBlock(){
293 | $this->blocked_at = $this->blocked_at==null?time():null;
294 | return $this->Save();
295 | }
296 |
297 |
298 | /**
299 | * Toggle administrator perimistion of user
300 | * @return boolean whether toogle success
301 | */
302 | public function toggleAdministrator(){
303 | $this->administrator = $this->administrator?0:1;
304 | return $this->Save();
305 | }
306 |
307 |
308 | /**
309 | * Check user is actived status
310 | *
311 | * @return boolean whether user is actived
312 | */
313 | public function isBlocked(){
314 | return $this->blocked_at !== null ;
315 | }
316 |
317 | /**
318 | * Check administrator permistion of user
319 | *
320 | * @return boolean whether user is super user
321 | */
322 | public function isAdministrator(){
323 | return $this->administrator == true;
324 | }
325 |
326 | /**
327 | * Validates password
328 | *
329 | * @param string $password password to validate
330 | * @return boolean if password provided is valid for current user
331 | */
332 | public function validatePassword($password)
333 | {
334 | return Yii::$app->security->validatePassword($password, $this->password_hash);
335 | }
336 |
337 | /**
338 | * Generates password hash from password and sets it to the model
339 | *
340 | * @param string $password
341 | */
342 | public function setPassword($password)
343 | {
344 | $this->password_hash = Yii::$app->security->generatePasswordHash($password);
345 | }
346 |
347 | /**
348 | * Find user by login field
349 | *
350 | * @param string $email email to find
351 | * @return boolean|UserAccounts
352 | */
353 | public static function findIdentityByLogin($login){
354 | $model = static::findOne(['login'=>$login]);
355 | return $model;
356 | }
357 |
358 | /**
359 | * Finds an identity by the given ID.
360 | * @param string|integer $id the ID to be looked for
361 | * @return IdentityInterface the identity object that matches the given ID.
362 | * Null should be returned if such an identity cannot be found
363 | * or the identity is not in an active state (disabled, deleted, etc.)
364 | */
365 | public static function findIdentity($id) {
366 | return static::findOne($id);
367 | }
368 |
369 | /**
370 | * Returns an ID that can uniquely identify a user identity.
371 | * @return string|integer an ID that uniquely identifies a user identity.
372 | */
373 | public function getId() {
374 | return $this->id;
375 | }
376 |
377 | /**
378 | * Finds an identity by the given token.
379 | * @param mixed $token the token to be looked for
380 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
381 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
382 | * @return IdentityInterface the identity object that matches the given token.
383 | * Null should be returned if such an identity cannot be found
384 | * or the identity is not in an active state (disabled, deleted, etc.)
385 | */
386 | public static function findIdentityByAccessToken($token, $type = null) {
387 | throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
388 | }
389 |
390 | /**
391 | * Returns a key that can be used to check the validity of a given identity ID.
392 | *
393 | * The key should be unique for each individual user, and should be persistent
394 | * so that it can be used to check the validity of the user identity.
395 | *
396 | * The space of such keys should be big enough to defeat potential identity attacks.
397 | *
398 | * This is required if [[User::enableAutoLogin]] is enabled.
399 | * @return string a key that is used to check the validity of a given identity ID.
400 | * @see validateAuthKey()
401 | */
402 | public function getAuthKey() {
403 | return $this->auth_key;
404 | }
405 |
406 | /**
407 | * Validates the given auth key.
408 | *
409 | * This is required if [[User::enableAutoLogin]] is enabled.
410 | * @param string $authKey the given auth key
411 | * @return boolean whether the given auth key is valid.
412 | * @see getAuthKey()
413 | */
414 | public function validateAuthKey($authKey) {
415 | return $this->getAuthKey() === $authKey;
416 | }
417 |
418 | public static function tableName() {
419 | return 'user_accounts';
420 | }
421 |
422 | /**
423 | * Setup creator's ip is current client ip
424 | * @return void
425 | */
426 | protected function prepareCreatorIp(){
427 | if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
428 | $this->creator_ip = $_SERVER['HTTP_CLIENT_IP'];
429 | } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
430 | $this->creator_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
431 | } else {
432 | $this->creator_ip = $_SERVER['REMOTE_ADDR'];
433 | }
434 | }
435 |
436 |
437 |
438 | }
439 |
--------------------------------------------------------------------------------
/src/base/traits/AjaxValidationTrait.php:
--------------------------------------------------------------------------------
1 |
13 | * @since 1.0.0
14 | */
15 | trait AjaxValidationTrait
16 | {
17 |
18 | /**
19 | * Perform ajax validation.
20 | *
21 | * @param Model $model
22 | *
23 | * @throws \yii\base\ExitException
24 | */
25 | protected function performAjaxValidation(Model $model)
26 | {
27 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
28 | Yii::$app->response->format = Response::FORMAT_JSON;
29 | echo json_encode(ActiveForm::validate($model));
30 | Yii::$app->end();
31 | }
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/basic/EmailableInterface.php:
--------------------------------------------------------------------------------
1 |
8 | * @since 1.0.0
9 | */
10 | interface EmailableInterface {
11 |
12 | public function getEmail();
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/src/basic/Mailer.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class Mailer extends Object {
14 |
15 | /**
16 | * @var string
17 | */
18 | public $viewPath = '@userplus/basic/views/mails';
19 |
20 | /**
21 | * @var string|array The sender's email
22 | * Default: `Yii::$app->params['adminEmail']` OR `no-reply@example.com`
23 | */
24 | public $sender;
25 |
26 | /**
27 | * @var string The subject of welcome email
28 | */
29 | public $welcomeSubject;
30 |
31 | /**
32 | * @var string The subject of comfirmation and recomfirmation email
33 | */
34 | public $confirmationSubject;
35 |
36 | /**
37 | * @var string The subject of recovery password email
38 | */
39 | public $recoverySubject;
40 |
41 | /**
42 | * @var string The subject of reset password email
43 | */
44 | public $resetPasswordSubject;
45 |
46 | /**
47 | * Sends an email to a user after registration.
48 | *
49 | * @param EmailableInterface $user The user model implemented UserEmailSendable
50 | * @param array|null $data The data array pass to email view
51 | * @return bool
52 | */
53 | public function sendWelcomeMessage(EmailableInterface $user, array $data = null) {
54 | // Get email send to
55 | $email = $user->getEmail();
56 | return $this->send($email, $this->welcomeSubject, 'welcome', $data);
57 | }
58 |
59 | /**
60 | * Sends an email to a user with confirmation link.
61 | *
62 | * @param EmailableInterface $user The user model implemented UserEmailSendable
63 | * @param array|null $data The data array pass to email view
64 | *
65 | * @return bool
66 | */
67 | public function sendConfirmationMessage(EmailableInterface $user, array $data = null) {
68 | // Get email send to
69 | $email = $user->getEmail();
70 | // Send email
71 | return $this->send($email, $this->confirmationSubject, 'confirm', $data);
72 | }
73 |
74 | /**
75 | * Sends an email to a user with reconfirmation link.
76 | *
77 | * @param EmailableInterface $user The user model implemented UserEmailSendable
78 | * @param array|null $data The data array pass to email view
79 | *
80 | * @return bool
81 | */
82 | public function sendReconfirmationMessage(EmailableInterface $user, array $data = null) {
83 | return $this->sendConfirmationMessage($user, $data);
84 | }
85 |
86 | /**
87 | * Sends an email to a user with recovery link.
88 | *
89 | * @param EmailableInterface $user The user model implemented UserEmailSendable
90 | * @param array|null $data The data array pass to email view
91 | *
92 | * @return bool
93 | */
94 | public function sendRecoveryMessage(EmailableInterface $user, array $data = null) {
95 | // Get email send to
96 | $email = $user->getEmail();
97 | // Send email
98 | return $this->send($email, $this->recoverySubject, 'recovery', $data);
99 | }
100 |
101 | public function sendResetPasswordMessage(EmailableInterface $user, array $data = null) {
102 | // Get email send to
103 | $email = $user->getEmail();
104 | // Send email
105 | return $this->send($email, $this->resetPasswordSubject, 'reset', $data);
106 | }
107 |
108 | /**
109 | * Do send email email
110 | * @param string $to
111 | * @param string $subject
112 | * @param string $view
113 | * @param array|null $params
114 | *
115 | * @return bool
116 | */
117 | protected function send($to, $subject, $view, array $data = null) {
118 | /** @var \yii\mail\BaseMailer $mailer */
119 | $mailer = Yii::$app->mailer;
120 | $mailer->viewPath = $this->viewPath;
121 |
122 | if ($this->sender === null) {
123 | $this->sender = isset(Yii::$app->params['adminEmail']) ? Yii::$app->params['adminEmail'] : 'no-reply@example.com';
124 | }
125 |
126 | return $mailer->compose(['html' => 'html/' . $view, 'text' => 'text/' . $view], $data)
127 | ->setTo($to)
128 | ->setFrom($this->sender)
129 | ->setSubject($subject)
130 | ->send();
131 | }
132 |
133 | }
134 |
--------------------------------------------------------------------------------
/src/basic/Module.php:
--------------------------------------------------------------------------------
1 |
25 | * @since 1.0.0
26 | *
27 | */
28 | class Module extends BaseModule {
29 |
30 | public $enableUnconfirmedLogin = true;
31 | public $enableConfirmation = false;
32 | public $confirmWithin = 86400; // 24 hours
33 | public $confirmationHandlerRoute = '/user/security/confirm';
34 | public $enableRecoveryPassword = false;
35 | public $resetPasswordHandlerRoute = '/user/security/reset';
36 |
37 | /**
38 | *
39 | * @var integer The lenght of token use for recovery and confirmation
40 | */
41 | public $tokenLenght = 32;
42 |
43 | /**
44 | *
45 | * @var johnitvn\userplus\basic\Mailer The mailer instance
46 | */
47 | public $mailer;
48 |
49 | public function init() {
50 | parent::init();
51 | $mailer = \yii\helpers\ArrayHelper::merge($this->mailer, ['class' => 'johnitvn\userplus\basic\Mailer']);
52 | $this->mailer = \Yii::createObject($mailer);
53 | }
54 |
55 | public function getCommandControllerMap() {
56 | return [
57 | 'user' => $this->getConsoleControllerNamespace() . '\\UserController',
58 | ];
59 | }
60 |
61 | /**
62 | * Return default model map for modules.
63 | * When user not config model for map so we will get model class
64 | * from this default model map
65 | * @return array Default model map
66 | */
67 | protected function getDefaultModelMap() {
68 | $parentMap = parent::getDefaultModelMap();
69 | $parentMap['LoginForm'] = 'johnitvn\userplus\basic\models\LoginForm';
70 | $parentMap['RegisterForm'] = 'johnitvn\userplus\basic\models\RegisterForm';
71 | $parentMap['UserAccounts'] = 'johnitvn\userplus\basic\models\UserAccounts';
72 | $parentMap['RecoveryForm'] = 'johnitvn\userplus\basic\models\RecoveryForm';
73 | $parentMap['ResendForm'] = 'johnitvn\userplus\basic\models\ResendForm';
74 | return $parentMap;
75 | }
76 |
77 | /**
78 | * Return web controller namespace.
79 | * @return string The web app controller namespace
80 | */
81 | protected function getWebControllerNamespace() {
82 | return 'johnitvn\userplus\basic\controllers';
83 | }
84 |
85 | /**
86 | * Return console controller namespace.
87 | * @return array The console app controller namespace
88 | */
89 | protected function getConsoleControllerNamespace() {
90 | return 'johnitvn\userplus\basic\commands';
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/src/basic/UserConfirmableInterface.php:
--------------------------------------------------------------------------------
1 |
7 | * @since 1.0.0
8 | */
9 | interface UserConfirmableInterface extends EmailableInterface{
10 |
11 | public function isConfirmed();
12 |
13 | public function resendConfirmation();
14 |
15 | public function confirm();
16 |
17 | public function generateConfirmToken();
18 |
19 | public static function findIdentityByConfirmToken($token);
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/basic/UserRecoveryableInterface.php:
--------------------------------------------------------------------------------
1 |
8 | * @since 1.0.0
9 | */
10 | interface UserRecoveryableInterface extends EmailableInterface{
11 |
12 | public function recovery();
13 |
14 | public function resetPassword();
15 |
16 | public function generateRecoveryToken();
17 |
18 | public static function findIdentityByRecoveryToken($token);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/basic/actions/CommandCreateAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class CommandCreateAction extends Command {
15 |
16 | /**
17 | * Create new administrator account.
18 | *
19 | * @return string result content
20 | */
21 | public function run() {
22 | $this->doCreateAdministrator(Yii::t('user', 'Email'), Yii::t('user', 'Username'), Yii::t('user', 'Password'));
23 | }
24 |
25 | /**
26 | * Do create administrator
27 | * @param string $loginAttributeLabel The login atrribute's label for prompt
28 | * @param string $passwordAttributeLabel The password atrribute's label for prompt
29 | */
30 | public function doCreateAdministrator($loginAttributeLabel, $usernameAtrributeLabel, $passwordAttributeLabel) {
31 | $login = $this->controller->prompt('Enter ' . $loginAttributeLabel . ':', ['required']);
32 | $username = $this->controller->prompt('Enter ' . $usernameAtrributeLabel . ':', ['required']);
33 | $password = $this->controller->prompt('Enter ' . $passwordAttributeLabel . ':', ['required']);
34 |
35 | $user = $this->userPlusModule->createModelInstance('UserAccounts', [
36 | 'login' => $login,
37 | 'password' => $password,
38 | 'username' => $username,
39 | 'scenario' => 'console-create',
40 | ]);
41 |
42 | if ($user->consoleCreate()) {
43 | $this->controller->stdout(Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN);
44 | } else {
45 | $this->controller->stdout(Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED);
46 | foreach ($user->errors as $errors) {
47 | foreach ($errors as $error) {
48 | $this->controller->stdout(' - ' . $error . "\n", Console::FG_RED);
49 | }
50 | }
51 | $this->promptToRetry($loginAttributeLabel, $usernameAtrributeLabel, $passwordAttributeLabel);
52 | }
53 | }
54 |
55 | /**
56 | * Prompt user to retry
57 | * @param string $loginAttributeLabel The login atrribute's label for prompt
58 | * @param string $passwordAttributeLabel The password atrribute's label for prompt
59 | */
60 | private function promptToRetry($loginAttributeLabel, $usernameAtrributeLabel, $passwordAttributeLabel) {
61 | $exit = strtolower($this->controller->prompt('Do you want to retry?[Yes|No]', ['default' => 'N']));
62 | if ($exit === "yes" || $exit === "y") {
63 | $this->doCreateAdministrator($loginAttributeLabel, $usernameAtrributeLabel, $passwordAttributeLabel);
64 | } else if ($exit == "no" || $exit == "n") {
65 | exit();
66 | } else {
67 | $this->promptToRetry($loginAttributeLabel, $usernameAtrributeLabel, $passwordAttributeLabel);
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/src/basic/actions/ConfirmAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class ConfirmAction extends Action {
15 |
16 | use AjaxValidationTrait;
17 |
18 | /**
19 | * @var string the view file to be rendered. If not set, it will take the value of [[id]].
20 | * That means, if you name the action as "error" in "SiteController", then the view name
21 | * would be "error", and the corresponding view file would be "views/site/error.php".
22 | */
23 | public $view;
24 |
25 | /**
26 | * Runs the confirm action
27 | *
28 | * @return string result content
29 | */
30 | public function run($token) {
31 | $userClassName = $this->userPlusModule->getModelClassName('UserAccounts');
32 | $view = $this->view == null ? $this->id : $this->view;
33 | if (($model = call_user_func($userClassName . '::findIdentityByConfirmToken', $token)) !== null) {
34 | if ($this->userPlusModule->confirmWithin != false) {
35 | $time = explode("$", $token)[0];
36 | $waitTime = time() - intval($time);
37 | $confirmWithin = $this->userPlusModule->confirmWithin;
38 | if ($waitTime > $confirmWithin) {
39 | return $this->controller->render($view, [
40 | 'success' => false,
41 | 'message' => Yii::t('user', 'The confirmation link is invalid or expired. Please try requesting a new one.'),
42 | ]);
43 | }
44 | }
45 |
46 | if ($model->confirm()) {
47 | return $this->controller->render($view, [
48 | 'success' => true,
49 | 'message' => Yii::t('user', 'Thank you, registration is now complete.'),
50 | ]);
51 | } else {
52 | return $this->controller->render($view, [
53 | 'success' => false,
54 | 'message' => Yii::t('user','Something went wrong and your account has not been confirmed.'),
55 | ]);
56 | }
57 |
58 | } else {
59 | $view = $this->view == null ? $this->id : $this->view;
60 | return $this->controller->render($view, [
61 | 'success' => false,
62 | 'message' => Yii::t('user','Something went wrong and your account has not been confirmed.')
63 | ]);
64 | }
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/basic/actions/RecoveryPasswordAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class RecoveryPasswordAction extends Action {
15 |
16 | use AjaxValidationTrait;
17 |
18 | /**
19 | * @var string the view file to be rendered. If not set, it will take the value of [[id]].
20 | * That means, if you name the action as "error" in "SiteController", then the view name
21 | * would be "error", and the corresponding view file would be "views/site/error.php".
22 | */
23 | public $view;
24 |
25 | /**
26 | * Runs the reset password action
27 | *
28 | * @return string result content
29 | */
30 | public function run() {
31 | $model = $this->userPlusModule->createModelInstance('RecoveryForm');
32 |
33 | $this->performAjaxValidation($model);
34 | $view = $this->view == null ? $this->id : $this->view;
35 |
36 | if ($model->load(Yii::$app->request->post()) && $model->recovery()) {
37 | return $this->controller->render($view, [
38 | 'alert' => true,
39 | 'model' => $model,
40 | ]);
41 | } else {
42 | return $this->controller->render($view, [
43 | 'alert' => false,
44 | 'model' => $model,
45 | ]);
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/basic/actions/ResendConfirmAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class ResendConfirmAction extends Action {
15 |
16 | use AjaxValidationTrait;
17 |
18 | /**
19 | * @var string the view file to be rendered. If not set, it will take the value of [[id]].
20 | * That means, if you name the action as "error" in "SiteController", then the view name
21 | * would be "error", and the corresponding view file would be "views/site/error.php".
22 | */
23 | public $view;
24 |
25 | /**
26 | * Runs the reset password action
27 | *
28 | * @return string result content
29 | */
30 | public function run() {
31 | $model = $this->userPlusModule->createModelInstance('ResendForm');
32 |
33 | $this->performAjaxValidation($model);
34 | $view = $this->view == null ? $this->id : $this->view;
35 | $model->load(Yii::$app->request->post());
36 | if ($model->load(Yii::$app->request->post()) && $model->resendConfirmation()) {
37 | return $this->controller->render($view, [
38 | 'alert' => true,
39 | 'model' => $model,
40 | ]);
41 | } else {
42 | return $this->controller->render($view, [
43 | 'alert' => false,
44 | 'model' => $model,
45 | ]);
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/basic/actions/ResetPasswordAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class ResetPasswordAction extends Action {
15 |
16 | use AjaxValidationTrait;
17 |
18 | /**
19 | * @var string the view file to be rendered. If not set, it will take the value of [[id]].
20 | * That means, if you name the action as "error" in "SiteController", then the view name
21 | * would be "error", and the corresponding view file would be "views/site/error.php".
22 | */
23 | public $view;
24 |
25 | /**
26 | * Runs the reset password action
27 | *
28 | * @return string result content
29 | */
30 | public function run($token) {
31 | $modelClassName = $this->userPlusModule->getModelClassName('UserAccounts');
32 | $view = $this->view == null ? $this->id : $this->view;
33 | if (($userModel = call_user_func($modelClassName . '::findIdentityByRecoveryToken', $token)) !== null && $userModel->resetPassword()) {
34 | return $this->controller->render($view, [
35 | 'success' => true,
36 | 'message' => Yii::t("user", 'The new password will send to your email. Please change your password after login'),
37 | ]);
38 | } else {
39 | return $this->controller->render($view, [
40 | 'success' => false,
41 | 'message' => Yii::t("user", 'Something went wrong and your account can not reset password please retry request again.'),
42 | ]);
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/basic/commands/UserController.php:
--------------------------------------------------------------------------------
1 |
9 | * @since 1.0.0
10 | */
11 | class UserController extends ConsoleController{
12 |
13 | /**
14 | * @inheritdoc
15 | */
16 | public function actions() {
17 | return [
18 | 'create-admin'=>'johnitvn\userplus\basic\actions\CommandCreateAction',
19 | ];
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/basic/controllers/ManagerController.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class ManagerController extends BaseController {
14 |
15 | /**
16 | * @inheritdoc
17 | */
18 | public function behaviors() {
19 | $behaviors = parent::behaviors();
20 | $behaviors['verbs']['actions']['hand-confirm'] = ['post'];
21 | return $behaviors;
22 | }
23 |
24 | /**
25 | * @inheritdoc
26 | */
27 | public function actionHandConfirm($id) {
28 | $model = $this->findModel($id);
29 | $model->scenario = 'confirm';
30 | $model->confirm();
31 | Yii::$app->response->format = Response::FORMAT_JSON;
32 | return ['forceClose' => true, 'forceReload' => true];
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/basic/controllers/SecurityController.php:
--------------------------------------------------------------------------------
1 |
10 | * @since 1.0.0
11 | */
12 | class SecurityController extends BaseController {
13 |
14 | protected $registerView = '@userplus/basic/views/security/register';
15 |
16 | protected $loginView = '@userplus/basic/views/security/login';
17 |
18 | /**
19 | * @inheritdoc
20 | */
21 | public function actions() {
22 | $actions = parent::actions();
23 | $actions['confirm'] = 'johnitvn\userplus\basic\actions\ConfirmAction';
24 | $actions['recovery'] = 'johnitvn\userplus\basic\actions\RecoveryPasswordAction';
25 | $actions['reset'] = 'johnitvn\userplus\basic\actions\ResetPasswordAction';
26 | $actions['resend'] = 'johnitvn\userplus\basic\actions\ResendConfirmAction';
27 | return $actions;
28 | }
29 |
30 | /**
31 | * @inheritdoc
32 | */
33 | public function behaviors() {
34 | $behaviors = parent::behaviors();
35 | $behaviors['access']['only'][] = 'confirm';
36 | $behaviors['access']['only'][] = 'recovery';
37 | $behaviors['access']['only'][] = 'reset';
38 | $behaviors['access']['only'][] = 'resend';
39 | $behaviors['access']['rules'][] = [
40 | 'actions' => ['confirm', 'recovery', 'reset', 'resend'],
41 | 'allow' => true,
42 | 'roles' => ['?'],
43 | ];
44 | return $behaviors;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/basic/migrations/m150703_191015_init.php:
--------------------------------------------------------------------------------
1 | createTable('user_accounts', [
10 | 'id' => Schema::TYPE_PK,
11 | 'login' => Schema::TYPE_STRING . '(255) NOT NULL',
12 | 'username' => Schema::TYPE_STRING . '(255) NOT NULL',
13 | 'password_hash' => Schema::TYPE_STRING . '(255) NOT NULL',
14 | 'auth_key' => Schema::TYPE_STRING . '(255) NOT NULL',
15 | 'administrator' => Schema::TYPE_INTEGER,
16 | 'creator' => Schema::TYPE_INTEGER,
17 | 'creator_ip' => Schema::TYPE_STRING . '(40)',
18 | 'confirm_token' => Schema::TYPE_STRING,
19 | 'recovery_token' => Schema::TYPE_STRING,
20 | 'blocked_at' => Schema::TYPE_INTEGER,
21 | 'confirmed_at' => Schema::TYPE_INTEGER,
22 | 'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
23 | 'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
24 | ], $this->tableOptions);
25 |
26 | $this->createIndex('user_unique_login', 'user_accounts', 'login', true);
27 | $this->createIndex('user_unique_username', 'user_accounts', 'username', true);
28 | }
29 |
30 | public function down() {
31 | $this->dropTable('user_accounts');
32 | return true;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/basic/models/LoginForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class LoginForm extends BaseLoginForm {
14 |
15 | public function rules(){
16 | $rules = parent::rules();
17 | $rules['accountConfirmed'] = [
18 | 'login',
19 | function ($attribute) {
20 | if ($this->user!==null&&!$this->userPlusModule->enableUnconfirmedLogin&&!$this->user->isConfirmed()) {
21 | $this->addError($attribute, Yii::t('user', 'Your account is not confirmed'));
22 | }
23 | }
24 | ];
25 | return $rules;
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/basic/models/RecoveryForm.php:
--------------------------------------------------------------------------------
1 |
10 | * @since 1.0.0
11 | */
12 | class RecoveryForm extends Model {
13 |
14 | /** @var string User's plain password */
15 | public $login;
16 |
17 |
18 | /** @inheritdoc */
19 | public function attributeLabels()
20 | {
21 | return [
22 | 'login' => Yii::t('user', 'Email'),
23 | ];
24 | }
25 |
26 | /** @inheritdoc */
27 | public function formName()
28 | {
29 | return 'recovery-form';
30 | }
31 |
32 | /** @inheritdoc */
33 | public function rules()
34 | {
35 | return [
36 | 'requiredFields' => [['login'], 'required'],
37 | 'loginTrim' => ['login', 'trim'],
38 | 'loginPattern' => ['login', 'email'],
39 | ];
40 | }
41 |
42 | public function recovery(){
43 | if(!$this->validate()){
44 | return false;
45 | }else{
46 | $modelClass = $this->userPlusModule->getModelClassName('UserAccounts');
47 | $user = call_user_func($modelClass.'::findIdentityByEmail',$this->login);
48 | if($user!==null){
49 | $user->scenario = 'recovery';
50 | return $user->recovery();
51 | }else{
52 | $this->addError('login',Yii::t("user","We didn't found any account corresponds with this email"));
53 | }
54 | }
55 | }
56 |
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/basic/models/RegisterForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class RegisterForm extends BaseRegisterForm {
14 |
15 | public $username;
16 |
17 | public function attributeLabels() {
18 | $labels = parent::attributeLabels();
19 | $labels['login'] = Yii::t('user', 'Email');
20 | $labels['username'] = Yii::t('user', 'Username');
21 | return $labels;
22 | }
23 |
24 | public function rules() {
25 | $rules = parent::rules();
26 |
27 | $rules['loginPattern'] = ['login', 'email'];
28 |
29 | $rules['usernameRequired'] = ['username', 'required'];
30 | $rules['usernamePattern'] = ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'];
31 | $rules['usernameLength'] = ['username', 'string', 'min' => 3, 'max' => 255];
32 |
33 | return $rules;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/basic/models/ResendForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class ResendForm extends Model {
14 |
15 | /** @var string User's plain password */
16 | public $login;
17 |
18 | /** @inheritdoc */
19 | public function attributeLabels() {
20 | return [
21 | 'login' => Yii::t('user', 'Email'),
22 | ];
23 | }
24 |
25 | /** @inheritdoc */
26 | public function formName() {
27 | return 'resend-form';
28 | }
29 |
30 | /** @inheritdoc */
31 | public function rules() {
32 | return [
33 | 'requiredFields' => [['login'], 'required'],
34 | 'loginTrim' => ['login', 'trim'],
35 | 'loginPattern' => ['login', 'email'],
36 | ];
37 | }
38 |
39 | /**
40 | * Resend confirmation email to user.
41 | * If use already confirmed. Don't accept resend confirm email request
42 | * @return boolean
43 | */
44 | public function resendConfirmation() {
45 | if (!$this->validate()) {
46 | return false;
47 | } else {
48 | $modelClass = $this->userPlusModule->getModelClassName('UserAccounts');
49 | $user = call_user_func($modelClass . '::findIdentityByEmail', $this->login);
50 | if ($user !== null) {
51 | if ($user->confirmed_at !== null) {
52 | // user is confirmed
53 | $this->addError('login', Yii::t("user", "Your account is confirmed. You can login now"));
54 | return false;
55 | } else {
56 | $user->scenario = 'confirm';
57 | return $user->resendConfirmation();
58 | }
59 | } else {
60 | $this->addError('login', Yii::t("user", "We didn't found any account corresponds with this email"));
61 | }
62 | }
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/basic/models/UserAccounts.php:
--------------------------------------------------------------------------------
1 |
15 | * @since 1.0.0
16 | */
17 | class UserAccounts extends BaseUserAccounts implements UserConfirmableInterface, UserRecoveryableInterface {
18 |
19 | /**
20 | * @var User Accounts Event
21 | */
22 | const BEFORE_RESET_PASSWORD = 'beforeResetPassword';
23 |
24 | /**
25 | * @var User Accounts Event
26 | */
27 | const AFTER_RESET_PASSWORD = 'afterResetPassword';
28 |
29 | /**
30 | * @var User Accounts Event
31 | */
32 | const BEFORE_CONFIRM = 'beforeConfirm';
33 |
34 | /**
35 | * @var User Accounts Event
36 | */
37 | const AFTER_CONFIRM = 'afterConfirm';
38 |
39 | /**
40 | * @var User Accounts Event
41 | */
42 | const BEFORE_RECONFIRM = 'beforeReconfirm';
43 |
44 | /**
45 | * @var User Accounts Event
46 | */
47 | const AFTER_RECONFIRM = 'afterReconfirm';
48 |
49 | /**
50 | * @var User Accounts Event
51 | */
52 | const BEFORE_RECOVERY = 'beforeRecovery';
53 |
54 | /**
55 | * @var User Accounts Event
56 | */
57 | const AFTER_RECOVERY = 'afterRecovery';
58 |
59 | /**
60 | * Instace of johnitvn\userplus\basic\Mailer
61 | * @var johnitvn\userplus\basic\Mailer
62 | */
63 | protected $mailer;
64 |
65 | /**
66 | * @inheritdoc
67 | */
68 | public function init() {
69 | parent::init();
70 | $this->mailer = $this->userPlusModule->mailer;
71 | $this->on(self::AFTER_REGISTER, [$this, 'afterRegister']);
72 | $this->on(self::AFTER_RECOVERY, [$this, 'afterRecovery']);
73 | $this->on(self::AFTER_RESET_PASSWORD, [$this, 'afterResetPassword']);
74 | }
75 |
76 | /**
77 | * @inheritdoc
78 | */
79 | public function rules() {
80 | $rules = parent::rules();
81 | $rules['usernameRequired'] = ['username', 'required'];
82 | $rules['usernamePattern'] = ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'];
83 | $rules['usernameLength'] = ['username', 'string', 'min' => 3, 'max' => 255];
84 | $rules['usernameUnique'] = ['username', 'unique', 'message' => Yii::t('user', 'This username has already been taken')];
85 |
86 | $rules['loginUnique'] = ['login', 'unique', 'message' => Yii::t('user', 'This email has already been taken for other account')];
87 | $rules['loginPattern'] = ['login', 'email'];
88 | return $rules;
89 | }
90 |
91 | /**
92 | * @inheritdoc
93 | */
94 | public function attributeLabels() {
95 | $labels = parent::attributeLabels();
96 | $labels['login'] = Yii::t('user', 'Email');
97 | $labels['username'] = Yii::t('user', 'Username');
98 | return $labels;
99 | }
100 |
101 | public function scenarios() {
102 | $scenarios = parent::scenarios();
103 | $scenarios['register'][] = 'username';
104 | $scenarios['create'][] = 'username';
105 | $scenarios['console-create'][] = 'username';
106 | $scenarios['confirm'] = ['confirm_token', 'confirmed_at'];
107 | $scenarios['recovery'] = ['recovery_token'];
108 | $scenarios['reset-password'] = ['password'];
109 | return $scenarios;
110 | }
111 |
112 | /**
113 | * Get username of user
114 | * @return string Return the username of use
115 | */
116 | public function getUsername() {
117 | return $this->username;
118 | }
119 |
120 | /**
121 | * Do all jobs after register
122 | * @return boolean
123 | */
124 | public function afterRegister() {
125 | if ($this->userPlusModule->enableConfirmation) {
126 | $token = $this->generateConfirmToken();
127 | $route = $this->userPlusModule->confirmationHandlerRoute;
128 | $url = Url::to([$route, 'token' => $token], true);
129 | if (!$this->save()) {
130 | return false;
131 | }
132 | $this->mailer->sendConfirmationMessage($this, ['url' => $url]);
133 | }
134 | }
135 |
136 | /**
137 | * Find user by login field
138 | *
139 | * @param string email/username to find
140 | * @return boolean|UserAccounts
141 | */
142 | public static function findIdentityByLogin($login) {
143 | $userPlusModule = \Yii::$app->getModule('user');
144 | if ($userPlusModule->loginType == "username") {
145 | $model = static::findOne(['username' => $login]);
146 | } else {
147 | $model = static::findOne(['login' => $login]);
148 | }
149 | return $model;
150 | }
151 |
152 | public static function findIdentityByEmail($email) {
153 | return static::findOne(['login' => $email]);
154 | }
155 |
156 | /**
157 | *
158 | * @return boolean
159 | */
160 | public function isConfirmed() {
161 | return $this->confirmed_at !== null;
162 | }
163 |
164 | /**
165 | *
166 | * @return string
167 | */
168 | public function getEmail() {
169 | return $this->login;
170 | }
171 |
172 | /**
173 | *
174 | * @return boolean
175 | */
176 | public function confirm() {
177 | $this->trigger(self::BEFORE_CONFIRM);
178 | $this->scenario = 'confirm';
179 | $this->confirm_token = null;
180 | $this->confirmed_at = time();
181 | if (!$this->save()) {
182 | return false;
183 | }
184 | $this->trigger(self::AFTER_CONFIRM);
185 | return true;
186 | }
187 |
188 | /**
189 | *
190 | * @return boolean
191 | */
192 | public function recovery() {
193 | $this->trigger(self::BEFORE_RECOVERY);
194 | $this->generateRecoveryToken();
195 | if (!$this->save()) {
196 | return false;
197 | }
198 | $this->trigger(self::AFTER_RECOVERY);
199 | return true;
200 | }
201 |
202 | /**
203 | * Do all jobs when after recovery
204 | */
205 | public function afterRecovery() {
206 | $token = $this->recovery_token;
207 | $route = $this->userPlusModule->resetPasswordHandlerRoute;
208 | $url = Url::to([$route, 'token' => $token], true);
209 | $this->mailer->sendRecoveryMessage($this, ['url' => $url]);
210 | }
211 |
212 | /**
213 | *
214 | * @return boolean
215 | */
216 | public function resetPassword() {
217 | $this->trigger(self::BEFORE_RESET_PASSWORD);
218 | $this->scenario = 'reset-password';
219 | $this->password = Helper::generatePassword();
220 | if (!$this->save()) {
221 | return false;
222 | }
223 |
224 | $this->trigger(self::AFTER_RESET_PASSWORD);
225 | return true;
226 | }
227 |
228 | /**
229 | *
230 | */
231 | public function afterResetPassword() {
232 | $this->mailer->sendResetPasswordMessage($this, ['password' => $this->password]);
233 | }
234 |
235 | /**
236 | *
237 | * @return boolean
238 | */
239 | public function resendConfirmation() {
240 | $this->trigger(self::BEFORE_RECONFIRM);
241 | $token = $this->generateConfirmToken();
242 | $route = $this->userPlusModule->confirmationHandlerRoute;
243 | $url = Url::to([$route, 'token' => $token], true);
244 | if (!$this->save()) {
245 | return false;
246 | }
247 | $this->mailer->sendReconfirmationMessage($this, ['url' => $url]);
248 | $this->trigger(self::AFTER_RECONFIRM);
249 | return true;
250 | }
251 |
252 | /**
253 | *
254 | * @return string
255 | */
256 | public function generateConfirmToken() {
257 | $token = Helper::generateRandomString($this->userPlusModule->tokenLenght);
258 | $this->confirm_token = $token;
259 | return $this->confirm_token;
260 | }
261 |
262 | /**
263 | *
264 | * @return string
265 | */
266 | public function generateRecoveryToken() {
267 | $token = Helper::generateRandomString($this->userPlusModule->tokenLenght);
268 | $this->recovery_token = $token;
269 | return $this->recovery_token;
270 | }
271 |
272 | /**
273 | *
274 | * @param string $token
275 | * @return UserAccounts
276 | */
277 | public static function findIdentityByConfirmToken($token) {
278 | return UserAccounts::findOne(['confirm_token' => $token]);
279 | }
280 |
281 | /**
282 | *
283 | * @param string $token
284 | * @return UserAccounts
285 | */
286 | public static function findIdentityByRecoveryToken($token) {
287 | return UserAccounts::findOne(['recovery_token' => $token]);
288 | }
289 |
290 | }
291 |
--------------------------------------------------------------------------------
/src/basic/views/mails/html/confirm.php:
--------------------------------------------------------------------------------
1 |
4 |
5 | = Yii::t('user', 'Hello') ?>,
6 |
7 |
8 | = Yii::t('user', 'Thank you for signing up on {0}', Yii::$app->name) ?>.
9 | = Yii::t('user', 'In order to complete your registration, please click the link below') ?>.
10 |
11 |
12 | = Html::a($url, $url)."
"; ?>.
13 |
14 |
15 | = Yii::t('user', 'If you cannot click the link, please try pasting the text into your browser') ?>.
16 |
17 |
18 | = Yii::t('user', 'If you did not make this request you can ignore this email') ?>.
19 |
--------------------------------------------------------------------------------
/src/basic/views/mails/html/recovery.php:
--------------------------------------------------------------------------------
1 |
4 |
5 | = Yii::t('user', 'Hello') ?>,
6 |
7 |
8 | = Yii::t('user', 'We have received a request to reset the password for your account on {0}', Yii::$app->name) ?>.
9 | = Yii::t('user', 'Please click the link below to complete your password reset') ?>.
10 |
11 |
12 | = Html::a($url, $url); ?>
13 |
14 |
15 | = Yii::t('user', 'If you cannot click the link, please try pasting the text into your browser') ?>.
16 |
17 |
18 | = Yii::t('user', 'If you did not make this request you can ignore this email') ?>.
19 |
--------------------------------------------------------------------------------
/src/basic/views/mails/html/reset.php:
--------------------------------------------------------------------------------
1 |
4 |
5 | = Yii::t('user', 'Hello') ?>,
6 |
7 |
8 | = Yii::t('user', 'We have received a confirm of your request to reset the password for your account on {0}', Yii::$app->name) ?>.
9 | = Yii::t('user', 'This is your new password') ?>.
10 |
11 |
12 | = Yii::t('user', 'New Password: {0}',$password) ?>.
13 |
14 |
15 | = Yii::t('user', 'Please change password after you login for security.') ?>.
16 |
17 |
--------------------------------------------------------------------------------
/src/basic/views/mails/layouts/html.php:
--------------------------------------------------------------------------------
1 | beginPage() ?>
2 |
3 |
4 |
5 |
6 |
7 | head() ?>
8 |
9 |
10 |
11 |
12 | |
13 |
14 |
15 |
16 |
17 |
18 | beginBody() ?>
19 | = $content ?>
20 | endBody() ?>
21 | |
22 |
23 |
24 |
25 | |
26 | |
27 |
28 |
29 |
46 |
47 |
48 | endPage() ?>
--------------------------------------------------------------------------------
/src/basic/views/mails/layouts/text.php:
--------------------------------------------------------------------------------
1 | beginPage() ?>
2 | beginBody() ?>
3 | = $content ?>
4 | endBody() ?>
5 | endPage() ?>
--------------------------------------------------------------------------------
/src/basic/views/mails/text/confirm.php:
--------------------------------------------------------------------------------
1 | = Yii::t('user', 'Hello') ?>,
2 | = Yii::t('user', 'Thank you for signing up on {0}', Yii::$app->name) ?>.
3 | = Yii::t('user', 'In order to complete your registration, please click the link below') ?>.
4 | = $url ?>.
5 | = Yii::t('user', 'If you cannot click the link, please try pasting the text into your browser') ?>.
6 | = Yii::t('user', 'If you did not make this request you can ignore this email') ?>.
--------------------------------------------------------------------------------
/src/basic/views/mails/text/recovery.php:
--------------------------------------------------------------------------------
1 | = Yii::t('user', 'Hello') ?>,
2 |
3 | = Yii::t('user', 'We have received a request to reset the password for your account on {0}', Yii::$app->name) ?>.
4 | = Yii::t('user', 'Please click the link below to complete your password reset') ?>.
5 |
6 | = $url ?>
7 |
8 | = Yii::t('user', 'If you cannot click the link, please try pasting the text into your browser') ?>.
9 |
10 | = Yii::t('user', 'If you did not make this request you can ignore this email') ?>.
--------------------------------------------------------------------------------
/src/basic/views/mails/text/reset.php:
--------------------------------------------------------------------------------
1 | = Yii::t('user', 'Hello') ?>,
2 | = Yii::t('user', 'We have received a confirm of your request to reset the password for your account on {0}', Yii::$app->name) ?>.
3 | = Yii::t('user', 'This is your new password') ?>.
4 | = Yii::t('user', 'New Password: {0}',$password) ?>.
5 | = Yii::t('user', 'Please change password after you login for security.') ?>.
6 |
--------------------------------------------------------------------------------
/src/basic/views/manager/_columns.php:
--------------------------------------------------------------------------------
1 | 'kartik\grid\CheckboxColumn',
10 | 'width' => '20px',
11 | ],
12 | [
13 | 'class' => '\kartik\grid\DataColumn',
14 | 'attribute' => 'id',
15 | 'width' => '40px',
16 | ],
17 | [
18 | 'class' => '\kartik\grid\DataColumn',
19 | 'attribute' => 'username',
20 | ],
21 | [
22 | 'class' => '\kartik\grid\DataColumn',
23 | 'attribute' => 'login',
24 | ],
25 | [
26 | 'class' => '\kartik\grid\DataColumn',
27 | 'attribute' => 'created_at',
28 | 'value' => function($model) {
29 | return date('d/m/Y', $model->created_at);
30 | },
31 | 'filter' => DatePicker::widget([
32 | 'model' => $searchModel,
33 | 'attribute' => 'created_at',
34 | 'dateFormat' => 'php:Y-m-d',
35 | 'options' => [
36 | 'class' => 'form-control',
37 | ],
38 | ]),
39 | ],
40 | [
41 | 'class' => '\kartik\grid\DataColumn',
42 | 'width' => '50px',
43 | 'attribute' => 'confirmed_at',
44 | 'label'=>'Confirmation',
45 | 'value' => function ($model) {
46 | if ($model->confirmed_at === null) {
47 | return Html::a(Yii::t('user', 'Confirm'), ['hand-confirm', 'id' => $model->id], [
48 | 'class' => 'btn btn-xs btn-primary btn-block',
49 | 'role' => 'modal-remote',
50 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
51 | 'data-request-method' => 'post',
52 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
53 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to confirm for this user?'),
54 | ]);
55 | } else {
56 | return 'Confirmed';
57 | }
58 | },
59 | 'format' => 'raw'
60 | ],
61 | [
62 | 'class' => '\kartik\grid\DataColumn',
63 | 'width' => '50px',
64 | 'attribute' => 'blocked_at',
65 | 'label'=>'Status',
66 | 'value' => function ($model) {
67 | if ($model->blocked_at !== null) {
68 | return Html::a(Yii::t('user', 'Unblock'), ['toggle-block', 'id' => $model->id], [
69 | 'class' => 'btn btn-xs btn-warning btn-block',
70 | 'role' => 'modal-remote',
71 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
72 | 'data-request-method' => 'post',
73 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
74 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
75 | ]);
76 | } else {
77 | return Html::a(Yii::t('user', 'Block'), ['toggle-block', 'id' => $model->id], [
78 | 'class' => 'btn btn-xs btn-danger btn-block',
79 | 'role' => 'modal-remote',
80 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
81 | 'data-request-method' => 'post',
82 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
83 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
84 | ]);
85 | }
86 | },
87 | 'format' => 'raw'
88 | ],
89 | ];
90 |
91 |
92 | $rbacModule = Yii::$app->getModule('rbac');
93 |
94 | $columns[] = [
95 | 'class' => '\kartik\grid\DataColumn',
96 | 'width' => '130px',
97 | 'attribute' => 'administrator',
98 | 'value' => function ($model) {
99 | if (!$model->administrator) {
100 | return Html::a(Yii::t('user', 'Set SU'), ['toggle-superuser', 'id' => $model->id], [
101 | 'class' => 'btn btn-xs btn-danger btn-block',
102 | 'role' => 'modal-remote',
103 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
104 | 'data-request-method' => 'post',
105 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
106 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
107 | ]);
108 | } else {
109 | return Html::a(Yii::t('user', 'Remove SU'), ['toggle-superuser', 'id' => $model->id], [
110 | 'class' => 'btn btn-xs btn-info btn-block',
111 | 'role' => 'modal-remote',
112 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
113 | 'data-request-method' => 'post',
114 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
115 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
116 | ]);
117 | }
118 | },
119 | 'format' => 'raw',
120 | 'filter' => [0 => 'Not Admin', 1 => 'Admin'],
121 | ];
122 |
123 | if (get_class($rbacModule) === 'johnitvn\rbacplus\Module') {
124 | /**
125 | * Intergrate with Rbac Plus extension
126 | */
127 | $columns[] = [
128 | 'class' => 'kartik\grid\DataColumn',
129 | 'header' => Yii::t('rbac', 'Assignment'),
130 | 'hAlign' => 'center',
131 | 'value'=>function($model){
132 | return Html::a('',
133 | ['/rbac/assignment/assignment', 'id' => $model->id],
134 | [
135 | 'role' => 'modal-remote',
136 | 'title' => Yii::t('user', 'Assignment'),
137 | ]
138 | );
139 | },
140 | 'format' => 'raw',
141 | 'visible' => Yii::$app->user->identity->isAdministrator(),
142 | ];
143 |
144 | }
145 |
146 | $columns[] = [
147 | 'class' => 'kartik\grid\ActionColumn',
148 | 'dropdown' => false,
149 | 'vAlign' => 'middle',
150 | 'urlCreator' => function($action, $model, $key, $index) {
151 | return Url::to([$action, 'id' => $key]);
152 | },
153 | 'viewOptions' => ['role' => 'modal-remote', 'title' => 'View', 'data-toggle' => 'tooltip'],
154 | 'updateOptions' => ['role' => 'modal-remote', 'title' => 'Update', 'data-toggle' => 'tooltip'],
155 | 'deleteOptions' => ['role' => 'modal-remote', 'title' => 'Delete',
156 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
157 | 'data-request-method' => 'post',
158 | 'data-toggle' => 'tooltip',
159 | 'data-confirm-title' => 'Are you sure?',
160 | 'data-confirm-message' => 'Are you sure want to delete this item'],
161 | ];
162 |
163 | return $columns;
--------------------------------------------------------------------------------
/src/basic/views/manager/_form.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
27 |
--------------------------------------------------------------------------------
/src/basic/views/manager/create.php:
--------------------------------------------------------------------------------
1 |
2 | = $this->render('_form', [
3 | 'model' => $model,
4 | ]) ?>
5 |
6 |
--------------------------------------------------------------------------------
/src/basic/views/manager/index.php:
--------------------------------------------------------------------------------
1 | title = 'Users';
10 | $this->params['breadcrumbs'][] = $this->title;
11 |
12 | CrudAsset::register($this);
13 |
14 | ?>
15 |
16 |
17 | =GridView::widget([
18 | 'id'=>'crud-datatable',
19 | 'dataProvider' => $dataProvider,
20 | 'filterModel' => $searchModel,
21 | 'pjax'=>true,
22 | 'columns' => require(__DIR__.'/_columns.php'),
23 | 'toolbar'=> [
24 | ['content'=>
25 | Html::a('
', ['create'],
26 | ['role'=>'modal-remote','title'=> 'Create new Users','class'=>'btn btn-default']).
27 | Html::a('
', [''],
28 | ['data-pjax'=>1, 'class'=>'btn btn-default', 'title'=>'Reset Grid']).
29 | '{toggleData}'.
30 | '{export}'
31 | ],
32 | ],
33 | 'striped' => true,
34 | 'condensed' => true,
35 | 'responsive' => true,
36 | 'panel' => [
37 | 'type' => 'primary',
38 | 'heading' => '
Users listing',
39 | 'before'=>'
* Resize table columns just like a spreadsheet by dragging the column edges.',
40 | 'after'=>BulkButtonWidget::widget([
41 | 'buttons'=> Html::a('
Delete All',
42 | ["bulk-delete"] ,
43 | [
44 | "class"=>"btn btn-danger btn-xs",
45 | 'role'=>'modal-remote-bulk',
46 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
47 | 'data-request-method'=>'post',
48 | 'data-confirm-title'=>'Are you sure?',
49 | 'data-confirm-message'=>'Are you sure want to delete all this items'
50 | ]).' '.
51 | Html::a('
Block All',
52 | ["bulk-block"] ,
53 | [
54 | "class"=>"btn btn-danger btn-xs",
55 | 'role'=>'modal-remote-bulk',
56 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
57 | 'data-request-method'=>'post',
58 | 'data-confirm-title'=>'Are you sure?',
59 | 'data-confirm-message'=>'Are you sure want to block all this items'
60 | ]).' '.
61 | Html::a('
Unblock All',
62 | ["bulk-unblock"] ,
63 | [
64 | "class"=>"btn btn-warning btn-xs",
65 | 'role'=>'modal-remote-bulk',
66 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
67 | 'data-request-method'=>'post',
68 | 'data-confirm-title'=>'Are you sure?',
69 | 'data-confirm-message'=>'Are you sure want to unblock all this items'
70 | ]),
71 | ]).
72 | '
',
73 | ]
74 | ])?>
75 |
76 |
77 | "ajaxCrubModal",
79 | "footer"=>"",// always need it for jquery plugin
80 | ])?>
81 |
82 |
--------------------------------------------------------------------------------
/src/basic/views/manager/update.php:
--------------------------------------------------------------------------------
1 |
2 | = $this->render('_form', [
3 | 'model' => $model,
4 | ]) ?>
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/basic/views/manager/view.php:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 | = DetailView::widget([
11 | 'model' => $model,
12 | 'attributes' => [
13 | 'id',
14 | 'login',
15 | 'username',
16 | [
17 | 'attribute'=>'administrator',
18 | 'value'=> $model['administrator']?"Yes":"No"
19 | ],
20 | [
21 | 'attribute'=>'creator',
22 | 'format' => 'raw',
23 | 'value'=> $model['creator']==-1?"Created by Console":
24 | $model['creator']==-2?"User register by my self":
25 | Html::a(UserAccounts::findOne($model['creator'])->login,['/user/manager/view','id'=>UserAccounts::findOne($model['creator'])->id],["role"=>"modal-remote"])
26 | ],
27 | 'creator_ip',
28 | [
29 | 'attribute'=>'blocked_at',
30 | 'value'=> $model['blocked_at']==null?"Not blocked":date("d/m/Y H:i:s",$model['blocked_at'])
31 | ],
32 | [
33 | 'attribute'=>'confirmed_at',
34 | 'value'=>$model['confirmed_at']==null?'Unconfirmed':date("d/m/Y H:i:s",$model['confirmed_at'])
35 | ],
36 | [
37 | 'attribute'=>'created_at',
38 | 'value'=>date("d/m/Y H:i:s",$model['created_at'])
39 | ],
40 | [
41 | 'attribute'=>'updated_at',
42 | 'value'=>$model['updated_at']==-1?\Yii::t("user","Never Update"):date("d/m/Y H:i:s",$model['updated_at'])
43 | ],
44 | ],
45 | ]) ?>
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/basic/views/security/confirm.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Confirmation');
7 | ?>
8 |
9 |
10 | =$message?>
11 |
12 | =$message?>
13 |
14 |
--------------------------------------------------------------------------------
/src/basic/views/security/login.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Sign in');
7 | $module = Yii::$app->getModule('user');
8 | ?>
9 |
10 |
11 |
12 |
13 |
= Html::encode($this->title) ?>
14 |
15 |
16 | 'login-form',
18 | 'enableAjaxValidation' => true,
19 | 'enableClientValidation' => false,
20 | 'validateOnBlur' => false,
21 | 'validateOnType' => false,
22 | 'validateOnChange' => false,
23 | ]) ?>
24 |
25 | = $form->field($model, 'login', ['inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control', 'tabindex' => '1']]) ?>
26 |
27 | = $form->field($model, 'password', ['inputOptions' => ['class' => 'form-control', 'tabindex' => '2']])->passwordInput()->label(Yii::t('user', 'Password')) ?>
28 | enableRecoveryPassword): ?>
29 | = Html::a(Yii::t('user', 'Forgot password?'), ['/user/security/recovery']) ?>
30 |
31 | = $form->field($model, 'rememberMe')->checkbox(['tabindex' => '4']) ?>
32 |
33 | = Html::submitButton(Yii::t('user', 'Sign in'), ['class' => 'btn btn-primary btn-block', 'tabindex' => '3']) ?>
34 |
35 |
36 |
37 |
38 | enableRegister): ?>
39 |
40 | = Html::a(Yii::t('user', 'Don\'t have an account? Sign up!'), ['/user/security/register']) ?>
41 |
42 |
43 | enableConfirmation): ?>
44 |
45 | = Html::a(Yii::t('user', 'Didn\'t receive confirmation message?'), ['/user/security/resend']) ?>
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/src/basic/views/security/recovery.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Recovery Password');
5 | $module = Yii::$app->getModule('user');
6 |
7 | if($alert):?>
8 |
9 |
10 |
11 |
12 | =\Yii::t('user',"We have send recovery link to your email.Please check it!") ?>
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
= Html::encode($this->title) ?>
21 |
22 |
23 | 'recovery-form',
25 | 'enableAjaxValidation' => true,
26 | 'enableClientValidation' => false,
27 | ]); ?>
28 |
29 | = $form->field($model, 'login') ?>
30 |
31 | = Html::submitButton(Yii::t('user', 'Recovery Password'), ['class' => 'btn btn-primary btn-block']) ?>
32 |
33 |
34 |
35 |
36 | =Html::a(Yii::t('user', 'Already registered? Sign in!'), ['/user/security/login']) ?>
37 |
38 | enableRegister): ?>
39 |
40 | = Html::a(Yii::t('user', 'Don\'t have an account? Sign up!'), ['/user/security/register']) ?>
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/basic/views/security/register.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Sign up');
5 | ?>
6 |
7 |
8 |
9 |
10 |
= Html::encode($this->title) ?>
11 |
12 |
13 | 'registration-form',
15 | 'enableAjaxValidation' => true,
16 | 'enableClientValidation' => false,
17 | ]); ?>
18 |
19 | = $form->field($model, 'username') ?>
20 |
21 | = $form->field($model, 'login') ?>
22 |
23 | = $form->field($model, 'password')->passwordInput() ?>
24 |
25 | = $form->field($model, 'confirm_password')->passwordInput() ?>
26 |
27 | = Html::submitButton(Yii::t('user', 'Sign up'), ['class' => 'btn btn-primary btn-block']) ?>
28 |
29 |
30 |
31 |
32 |
33 | = Html::a(Yii::t('user', 'Already registered? Sign in!'), ['/user/security/login']) ?>
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/basic/views/security/resend.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Resend confirm email');
5 | $module = Yii::$app->getModule('user');
6 |
7 | if($alert):?>
8 |
9 |
10 |
11 |
12 | =\Yii::t('user',"We have resend confirm email. Please check your email!") ?>
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
= Html::encode($this->title) ?>
21 |
22 |
23 | 'resend-form',
25 | 'enableAjaxValidation' => true,
26 | 'enableClientValidation' => false,
27 | ]); ?>
28 |
29 | = $form->field($model, 'login') ?>
30 |
31 | = Html::submitButton(Yii::t('user', 'Resend'), ['class' => 'btn btn-primary btn-block']) ?>
32 |
33 |
34 |
35 |
36 | =Html::a(Yii::t('user', 'Already registered? Sign in!'), ['/user/security/login']) ?>
37 |
38 | enableRegister): ?>
39 |
40 | = Html::a(Yii::t('user', 'Don\'t have an account? Sign up!'), ['/user/security/register']) ?>
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/basic/views/security/reset.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Reset Password');
3 | ?>
4 |
5 |
6 | =$message?>
7 |
8 | =$message?>
9 |
10 |
--------------------------------------------------------------------------------
/src/messages/vi/user.php:
--------------------------------------------------------------------------------
1 |
21 | * @since 1.0.0
22 | *
23 | */
24 | class Module extends BaseModule {
25 |
26 | /**
27 | * @var array modelMap Model mapping.
28 | * Need for customize model.
29 | * Simple module required three model UserAccounts,LoginForm,RegisterForm
30 | * ````php
31 | * [
32 | * 'UserSearch'=>'',
33 | * 'UserAccounts'=>'',
34 | * 'LoginForm'=>'',
35 | * 'RegisterForm'=>''
36 | * ]
37 | * ````
38 | */
39 | public $modelMap = [];
40 |
41 | /**
42 | * @var string The login type accept "email"/"username".
43 | * Default is username
44 | */
45 | public $loginType = "username";
46 |
47 | /**
48 | * Initial module
49 | * @return void
50 | */
51 | public function init() {
52 | parent::init();
53 | if ($this->loginType !== "username" & $this->loginType !== "email") {
54 | throw new yii\base\InvalidConfigException('loginType just accept "username"/"email".');
55 | }
56 | }
57 |
58 | public function getCommandControllerMap() {
59 | return [
60 | 'user' => $this->getConsoleControllerNamespace() . '\\UserController',
61 | ];
62 | }
63 |
64 | /**
65 | * Return default model map for modules.
66 | * When user not config model for map so we will get model class
67 | * from this default model map
68 | * @return array Default model map
69 | */
70 | protected function getDefaultModelMap() {
71 | return [
72 | 'UserSearch' => 'johnitvn\userplus\simple\models\UserSearch',
73 | 'UserAccounts' => 'johnitvn\userplus\simple\models\UserAccounts',
74 | 'LoginForm' => 'johnitvn\userplus\simple\models\LoginForm',
75 | 'RegisterForm' => 'johnitvn\userplus\simple\models\RegisterForm',
76 | 'ChangePasswordForm' => 'johnitvn\userplus\simple\models\ChangePasswordForm',
77 | ];
78 | }
79 |
80 | /**
81 | * Return console controller namespace.
82 | * @return array The console app controller namespace
83 | */
84 | protected function getConsoleControllerNamespace() {
85 | return 'johnitvn\userplus\simple\commands';
86 | }
87 |
88 | /**
89 | * Return web controller namespace.
90 | * @return string The web app controller namespace
91 | */
92 | protected function getWebControllerNamespace() {
93 | return 'johnitvn\userplus\simple\controllers';
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/src/simple/actions/CommandCreateAction.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class CommandCreateAction extends Command {
15 |
16 | /**
17 | * Create new administrator account.
18 | *
19 | * @return string result content
20 | */
21 | public function run() {
22 | $this->doCreateAdministrator(Yii::t('user', 'Login'), Yii::t('user', 'Password'));
23 | }
24 |
25 | /**
26 | * Do create administrator
27 | * @param string $loginAttributeLabel The login atrribute's label for prompt
28 | * @param string $passwordAttributeLabel The password atrribute's label for prompt
29 | */
30 | public function doCreateAdministrator($loginAttributeLabel, $passwordAttributeLabel) {
31 | $login = $this->controller->prompt('Enter ' . $loginAttributeLabel . ':', ['required']);
32 | $password = $this->controller->prompt('Enter ' . $passwordAttributeLabel . ':', ['required']);
33 |
34 | $user = $this->userPlusModule->createModelInstance('UserAccounts', [
35 | 'login' => $login,
36 | 'password' => $password,
37 | 'scenario' => 'console-create',
38 | ]);
39 |
40 | if ($user->consoleCreate()) {
41 | $this->controller->stdout(Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN);
42 | } else {
43 | $this->controller->stdout(Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED);
44 |
45 | foreach ($user->errors as $errors) {
46 | foreach ($errors as $error) {
47 | $this->controller->stdout(' - ' . $error . "\n", Console::FG_RED);
48 | }
49 | }
50 | $this->promptToRetry($loginAttributeLabel, $passwordAttributeLabel);
51 | }
52 | }
53 |
54 | /**
55 | * Prompt user to retry
56 | * @param string $loginAttributeLabel The login atrribute's label for prompt
57 | * @param string $passwordAttributeLabel The password atrribute's label for prompt
58 | */
59 | private function promptToRetry($loginAttributeLabel, $passwordAttributeLabel) {
60 | $exit = $this->controller->prompt('Do you want to retry?[Yes|No]', ['default' => 'N']);
61 | $exit = strtolower($exit);
62 | if ($exit == "yes" || $exit == "y") {
63 | $this->doCreateAdministrator($loginAttributeLabel, $passwordAttributeLabel);
64 | } else if ($exit == "no" || $exit == "n") {
65 | exit();
66 | } else {
67 | $this->promptToRetry($loginAttributeLabel, $passwordAttributeLabel);
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/src/simple/commands/UserController.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class UserController extends ConsoleController {
14 |
15 | /**
16 | * @inheritdoc
17 | */
18 | public function actions() {
19 | return [
20 | 'create-admin' => 'johnitvn\userplus\simple\actions\CommandCreateAction',
21 | ];
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/simple/controllers/ManagerController.php:
--------------------------------------------------------------------------------
1 |
16 | * @since 1.0.0
17 | */
18 | class ManagerController extends WebController {
19 |
20 | /**
21 | * @inheritdoc
22 | */
23 | public function beforeAction($action) {
24 | if (!parent::beforeAction($action)) {
25 | return false;
26 | } else if (Yii::$app->user->isGuest || !Yii::$app->user->identity->isAdministrator()) {
27 | throw new \yii\web\ForbiddenHttpException(Yii::t('user', 'You are not allowed to perform this action.'));
28 | } else {
29 | return true;
30 | }
31 | }
32 |
33 | /**
34 | * @inheritdoc
35 | */
36 | public function behaviors() {
37 | return [
38 | 'verbs' => [
39 | 'class' => VerbFilter::className(),
40 | 'actions' => [
41 | 'delete' => ['post'],
42 | 'bulk-delete' => ['post'],
43 | 'toggle-block' => ['post'],
44 | 'toggle-superuser' => ['post'],
45 | ],
46 | ],
47 | ];
48 | }
49 |
50 | /**
51 | * Lists all UserAccounts models.
52 | * @return mixed
53 | */
54 | public function actionIndex() {
55 | $searchModel = $this->userPlusModule->createModelInstance('UserSearch');
56 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
57 |
58 | return $this->render('index', [
59 | 'searchModel' => $searchModel,
60 | 'dataProvider' => $dataProvider,
61 | ]);
62 | }
63 |
64 | /**
65 | * Displays a single UserAccounts model.
66 | * @param integer $id
67 | * @return mixed
68 | */
69 | public function actionView($id) {
70 | $request = Yii::$app->request;
71 | if ($request->isAjax) {
72 | Yii::$app->response->format = Response::FORMAT_JSON;
73 | return [
74 | 'title' => "User #" . $id,
75 | 'content' => $this->renderPartial('view', [
76 | 'model' => $this->findModel($id),
77 | ]),
78 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
79 | Html::a('Edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
80 | ];
81 | } else {
82 | return $this->render('view', [
83 | 'model' => $this->findModel($id),
84 | ]);
85 | }
86 | }
87 |
88 | /**
89 | * Creates a new UserAccounts model.
90 | * For ajax request will return json object
91 | * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
92 | * @return mixed
93 | */
94 | public function actionCreate() {
95 | $request = Yii::$app->request;
96 | $model = $this->userPlusModule->createModelInstance('UserAccounts', ['scenario' => 'create']);
97 |
98 | if ($request->isAjax) {
99 | /*
100 | * Process for ajax request
101 | */
102 | Yii::$app->response->format = Response::FORMAT_JSON;
103 | if ($request->isGet) {
104 | return [
105 | 'title' => "Create new User",
106 | 'content' => $this->renderPartial('create', [
107 | 'model' => $model,
108 | ]),
109 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
110 | Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
111 | ];
112 | } else if ($model->load($request->post()) && $model->create(Yii::$app->user->getId())) {
113 | return [
114 | 'forceReload' => 'true',
115 | 'title' => "Create new User",
116 | 'content' => 'Create User success',
117 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
118 | Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
119 | ];
120 | } else {
121 | return [
122 | 'title' => "Create new User",
123 | 'content' => $this->renderPartial('create', [
124 | 'model' => $model,
125 | ]),
126 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
127 | Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
128 | ];
129 | }
130 | } else {
131 | /*
132 | * Process for non-ajax request
133 | */
134 | if ($model->load($request->post()) && $model->create(Yii::$app->user->getId())) {
135 | return $this->redirect(['view', 'id' => $model->id]);
136 | } else {
137 | return $this->render('create', [
138 | 'model' => $model,
139 | ]);
140 | }
141 | }
142 | }
143 |
144 | /**
145 | * Updates an existing UserAccounts model.
146 | * For ajax request will return json object
147 | * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
148 | * @param integer $id
149 | * @return mixed
150 | */
151 | public function actionUpdate($id) {
152 | $request = Yii::$app->request;
153 | $model = $this->findModel($id);
154 | $model->scenario = 'update';
155 |
156 | if ($request->isAjax) {
157 | /*
158 | * Process for ajax request
159 | */
160 | Yii::$app->response->format = Response::FORMAT_JSON;
161 | if ($request->isGet) {
162 | return [
163 | 'title' => "Update User #" . $id,
164 | 'content' => $this->renderPartial('update', [
165 | 'model' => $this->findModel($id),
166 | ]),
167 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
168 | Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
169 | ];
170 | } else if ($model->load($request->post()) && $model->save()) {
171 | return [
172 | 'forceReload' => 'true',
173 | 'title' => "User #" . $id,
174 | 'content' => $this->renderPartial('view', [
175 | 'model' => $this->findModel($id),
176 | ]),
177 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
178 | Html::a('Edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
179 | ];
180 | } else {
181 | return [
182 | 'title' => "Update User #" . $id,
183 | 'content' => $this->renderPartial('update', [
184 | 'model' => $this->findModel($id),
185 | ]),
186 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
187 | Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
188 | ];
189 | }
190 | } else {
191 | /*
192 | * Process for non-ajax request
193 | */
194 | if ($model->load($request->post()) && $model->save()) {
195 | return $this->redirect(['view', 'id' => $model->id]);
196 | } else {
197 | return $this->render('update', [
198 | 'model' => $model,
199 | ]);
200 | }
201 | }
202 | }
203 |
204 | /**
205 | * Delete an existing UserAccounts model.
206 | * For ajax request will return json object
207 | * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
208 | * @param integer $id
209 | * @return mixed
210 | */
211 | public function actionDelete($id) {
212 | $request = Yii::$app->request;
213 | if (Yii::$app->user->getId() == $id) {
214 | if ($request->isAjax) {
215 | Yii::$app->response->format = Response::FORMAT_JSON;
216 | return [
217 | 'title' => 'An error occurred',
218 | 'content' => 'You can not delete yourself',
219 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
220 | ];
221 | } else {
222 | return $this->redirect(['index']);
223 | }
224 | }
225 |
226 | //$this->findModel($id)->delete();
227 |
228 | if ($request->isAjax) {
229 | /*
230 | * Process for ajax request
231 | */
232 | $this->findModel($id)->delete();
233 | Yii::$app->response->format = Response::FORMAT_JSON;
234 | return ['forceClose' => true, 'forceReload' => true];
235 | } else {
236 | /*
237 | * Process for non-ajax request
238 | */
239 | return $this->redirect(['index']);
240 | }
241 | }
242 |
243 | public function actionToggleBlock($id) {
244 | $model = $this->findModel($id);
245 | $model->scenario = 'toggle-block';
246 |
247 | Yii::$app->response->format = Response::FORMAT_JSON;
248 |
249 | if (Yii::$app->user->getId() == $id) {
250 | return [
251 | 'title' => 'An error occurred',
252 | 'content' => 'You can not block yourself',
253 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
254 | ];
255 | }
256 |
257 |
258 | if ($model != null && $model->toggleBlock()) {
259 | return ['forceClose' => true, 'forceReload' => true];
260 | } else {
261 | return [
262 | 'title' => 'An error occurred',
263 | 'content' => 'Can not toggle block this user. Getting unknow error',
264 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
265 | ];
266 | return;
267 | }
268 | }
269 |
270 | public function actionToggleSuperuser($id) {
271 | $model = $this->findModel($id);
272 | $model->scenario = 'toggle-administrator';
273 |
274 | Yii::$app->response->format = Response::FORMAT_JSON;
275 |
276 | if (Yii::$app->user->getId() == $id) {
277 | return [
278 | 'title' => 'An error occurred',
279 | 'content' => 'You can not disable superuser of yourself',
280 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
281 | ];
282 | }
283 |
284 | Yii::$app->response->format = Response::FORMAT_JSON;
285 | if ($model != null && $model->toggleAdministrator()) {
286 | return ['forceClose' => true, 'forceReload' => true];
287 | } else {
288 | return [
289 | 'title' => 'An error occurred',
290 | 'content' => 'Can not toggle block this user. Getting unknow error',
291 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
292 | ];
293 | }
294 | }
295 |
296 | /**
297 | * Delete multiple existing UserAccounts model.
298 | * For ajax request will return json object
299 | * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
300 | * @param integer $id
301 | * @return mixed
302 | */
303 | public function actionBulkDelete() {
304 | $request = Yii::$app->request;
305 | $pks = json_decode($request->post('pks')); // Array or selected records primary keys
306 |
307 | if (in_array(Yii::$app->user->getId(), $pks)) {
308 | if ($request->isAjax) {
309 | Yii::$app->response->format = Response::FORMAT_JSON;
310 | return [
311 | 'title' => 'An error occurred',
312 | 'content' => 'You can not delete yourself. Please get our your account in your selection',
313 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
314 | ];
315 | } else {
316 | return $this->redirect(['index']);
317 | }
318 | }
319 |
320 | $userClassName = $this->userPlusModule->getModelClassName('UserAccounts');
321 | foreach (call_user_func($userClassName . '::findAll', $pks) as $model) {
322 | $model->delete();
323 | }
324 |
325 |
326 | if ($request->isAjax) {
327 | /*
328 | * Process for ajax request
329 | */
330 | Yii::$app->response->format = Response::FORMAT_JSON;
331 | return ['forceClose' => true, 'forceReload' => true];
332 | } else {
333 | /*
334 | * Process for non-ajax request
335 | */
336 | return $this->redirect(['index']);
337 | }
338 | }
339 |
340 | public function actionBulkBlock() {
341 | $request = Yii::$app->request;
342 | $pks = json_decode($request->post('pks')); // Array or selected records primary keys
343 |
344 |
345 | if (in_array(Yii::$app->user->getId(), $pks)) {
346 | if ($request->isAjax) {
347 | Yii::$app->response->format = Response::FORMAT_JSON;
348 | return [
349 | 'title' => 'An error occurred',
350 | 'content' => 'You can not block yourself. Please get our your account in your selection',
351 | 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
352 | ];
353 | } else {
354 | return $this->redirect(['index']);
355 | }
356 | }
357 |
358 | $userClassName = $this->userPlusModule->getModelClassName('UserAccounts');
359 | foreach (call_user_func($userClassName . '::findAll', $pks) as $model) {
360 | $model->scenario = 'block';
361 | $model->block();
362 | }
363 |
364 |
365 | if ($request->isAjax) {
366 | /*
367 | * Process for ajax request
368 | */
369 | Yii::$app->response->format = Response::FORMAT_JSON;
370 | return ['forceClose' => true, 'forceReload' => true];
371 | } else {
372 | /*
373 | * Process for non-ajax request
374 | */
375 | return $this->redirect(['index']);
376 | }
377 | }
378 |
379 | public function actionBulkUnblock() {
380 | $request = Yii::$app->request;
381 | $pks = json_decode($request->post('pks')); // Array or selected records primary keys
382 |
383 | $userClassName = $this->userPlusModule->getModelClassName('UserAccounts');
384 | foreach (call_user_func($userClassName . '::findAll', $pks) as $model) {
385 | $model->scenario = 'unblock';
386 | $model->unblock();
387 | }
388 |
389 |
390 | if ($request->isAjax) {
391 | /*
392 | * Process for ajax request
393 | */
394 | Yii::$app->response->format = Response::FORMAT_JSON;
395 | return ['forceClose' => true, 'forceReload' => true];
396 | } else {
397 | /*
398 | * Process for non-ajax request
399 | */
400 | return $this->redirect(['index']);
401 | }
402 | }
403 |
404 | /**
405 | * Finds the UserAccounts model based on its primary key value.
406 | * If the model is not found, a 404 HTTP exception will be thrown.
407 | * @param integer $id
408 | * @return johnitvn\userplus\base\models\UserAccounts the loaded model
409 | * @throws NotFoundHttpException if the model cannot be found
410 | */
411 | protected function findModel($id) {
412 | $userClassName = $this->userPlusModule->getModelClassName('UserAccounts');
413 | if (($model = call_user_func($userClassName . '::findOne', $id)) !== null) {
414 | return $model;
415 | } else {
416 | throw new NotFoundHttpException('The requested page does not exist.');
417 | }
418 | }
419 |
420 | }
421 |
--------------------------------------------------------------------------------
/src/simple/controllers/SecurityController.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class SecurityController extends WebController {
15 |
16 | /**
17 | *
18 | * @var string The view of login action
19 | */
20 | protected $loginView = '@userplus/simple/views/security/login';
21 |
22 | /**
23 | *
24 | * @var string The view of register action
25 | */
26 | protected $registerView = '@userplus/simple/views/security/register';
27 |
28 | /**
29 | *
30 | * @var string The view of change password action
31 | */
32 | protected $changePasswordView = '@userplus/simple/views/security/change-password';
33 |
34 | /**
35 | * @inheritdoc
36 | */
37 | public function behaviors() {
38 | return [
39 | 'access' => [
40 | 'class' => AccessControl::className(),
41 | 'only' => ['logout', 'change-password', 'login', 'register'],
42 | 'rules' => [
43 | [
44 | 'actions' => ['login', 'register'],
45 | 'allow' => true,
46 | 'roles' => ['?'],
47 | ],
48 | [
49 | 'actions' => ['logout', 'change-password'],
50 | 'allow' => true,
51 | 'roles' => ['@'],
52 | ],
53 | ],
54 | ],
55 | 'verbs' => [
56 | 'class' => VerbFilter::className(),
57 | 'actions' => [
58 | 'logout' => ['post'],
59 | ],
60 | ],
61 | ];
62 | }
63 |
64 | /**
65 | * @inheritdoc
66 | */
67 | public function actions() {
68 | return [
69 | 'logout' => [
70 | 'class' => 'johnitvn\userplus\base\actions\LogoutAction',
71 | ],
72 | 'login' => [
73 | 'class' => 'johnitvn\userplus\base\actions\LoginAction',
74 | 'view' => $this->loginView,
75 | ],
76 | 'register' => [
77 | 'class' => 'johnitvn\userplus\base\actions\RegisterAction',
78 | 'view' => $this->registerView,
79 | ],
80 | 'change-password' => [
81 | 'class' => 'johnitvn\userplus\base\actions\ChangePasswordAction',
82 | 'view' => $this->changePasswordView,
83 | ],
84 | ];
85 | }
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/src/simple/migrations/m150703_191015_init.php:
--------------------------------------------------------------------------------
1 | createTable('user_accounts', [
10 | 'id' => Schema::TYPE_PK,
11 | 'login' => Schema::TYPE_STRING . '(255) NOT NULL',
12 | 'password_hash' => Schema::TYPE_STRING . '(255) NOT NULL',
13 | 'auth_key' => Schema::TYPE_STRING . '(255) NOT NULL',
14 | 'administrator' => Schema::TYPE_INTEGER,
15 | 'creator' => Schema::TYPE_INTEGER,
16 | 'creator_ip' => Schema::TYPE_STRING . '(40)',
17 | 'confirm_token' => Schema::TYPE_STRING,
18 | 'recovery_token' => Schema::TYPE_STRING,
19 | 'blocked_at' => Schema::TYPE_INTEGER,
20 | 'confirmed_at' => Schema::TYPE_INTEGER,
21 | 'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
22 | 'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
23 | ], $this->tableOptions);
24 |
25 | $this->createIndex('user_unique_login', 'user_accounts', 'login', true);
26 | }
27 |
28 | public function down() {
29 | $this->dropTable('user_accounts');
30 | return true;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/simple/models/ChangePasswordForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class ChangePasswordForm extends BaseChangePasswordForm {
14 |
15 |
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/simple/models/LoginForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class LoginForm extends BaseLoginForm {
14 |
15 | /**
16 | * @inheritdoc
17 | */
18 | public function attributeLabels()
19 | {
20 | $labels = parent::attributeLabels();
21 |
22 | if($this->userPlusModule->loginType=="username"){
23 | $labels['login'] = Yii::t('user', 'Username');
24 | }else{
25 | $labels['login'] = Yii::t('user', 'Email');
26 | }
27 |
28 | return $labels;
29 | }
30 |
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/simple/models/RegisterForm.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | */
13 | class RegisterForm extends BaseRegisterForm {
14 |
15 | /**
16 | * @inheritdoc
17 | */
18 | public function attributeLabels()
19 | {
20 | $labels = parent::attributeLabels();
21 |
22 | if($this->userPlusModule->loginType=="username"){
23 | $labels['login'] = Yii::t('user', 'Username');
24 | }else{
25 | $labels['login'] = Yii::t('user', 'Email');
26 | }
27 |
28 | return $labels;
29 | }
30 |
31 | /**
32 | * @inheritdoc
33 | */
34 | public function rules(){
35 | $rules = parent::rules();
36 |
37 | if($this->userPlusModule->loginType=="username"){
38 | $rules['loginPattern'] = ['login', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'];
39 | $rules['loginLength'] = ['login', 'string', 'min' => 3, 'max' => 255];
40 | }else{
41 | $rules['loginPattern'] = ['login','email'];
42 | }
43 | return $rules;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/simple/models/UserAccounts.php:
--------------------------------------------------------------------------------
1 |
11 | * @since 1.0.0
12 | *
13 | * @property integer $id
14 | * @property string $login
15 | * @property string $username
16 | * @property string $password_hash
17 | * @property string $auth_key
18 | * @property integer $administrator
19 | * @property integer $creator
20 | * @property string $creator_ip
21 | * @property string $confirm_token
22 | * @property string $recovery_token
23 | * @property integer $blocked_at
24 | * @property integer $confirmed_at
25 | * @property integer $created_at
26 | * @property integer $updated_at
27 | */
28 | class UserAccounts extends BaseUserAccounts{
29 |
30 |
31 | /**
32 | * @inheritdoc
33 | */
34 | public function rules(){
35 | $rules = parent::rules();
36 |
37 | if($this->userPlusModule->loginType=="username"){
38 | $rules['loginPattern'] = ['login', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'];
39 | $rules['loginLength'] = ['login', 'string', 'min' => 3, 'max' => 255];
40 | }else{
41 | $rules['loginPattern'] = ['login','email'];
42 | }
43 |
44 | return $rules;
45 | }
46 |
47 | /**
48 | * @inheritdoc
49 | */
50 | public function attributeLabels()
51 | {
52 | $labels = parent::attributeLabels();
53 | if($this->userPlusModule->loginType=="username"){
54 | $labels['login'] = Yii::t('user', 'Username');
55 | }else{
56 | $labels['login'] = Yii::t('user', 'Email');
57 | }
58 | return $labels;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/simple/models/UserSearch.php:
--------------------------------------------------------------------------------
1 |
12 | * @since 1.0.0
13 | */
14 | class UserSearch extends UserAccounts
15 | {
16 | /**
17 | * @inheritdoc
18 | */
19 | public function rules()
20 | {
21 | return [
22 | [['id','creator', 'creator_ip', 'confirmed_at','administrator' ,'created_at', 'updated_at'], 'integer'],
23 | [['login', 'password'], 'safe'],
24 | ];
25 | }
26 |
27 | /**
28 | * @inheritdoc
29 | */
30 | public function scenarios()
31 | {
32 | // bypass scenarios() implementation in the parent class
33 | return Model::scenarios();
34 | }
35 |
36 | /**
37 | * Creates data provider instance with search query applied
38 | *
39 | * @param array $params
40 | *
41 | * @return ActiveDataProvider
42 | */
43 | public function search($params)
44 | {
45 | $query = UserAccounts::find();
46 |
47 | $dataProvider = new ActiveDataProvider([
48 | 'query' => $query,
49 | ]);
50 |
51 | $this->load($params);
52 |
53 | if (!$this->validate()) {
54 | return $dataProvider;
55 | }
56 |
57 | $query->andFilterWhere([
58 | 'id' => $this->id,
59 | 'creator'=> $this->creator,
60 | 'creator_ip' => $this->creator_ip,
61 | 'confirmed_at' => $this->confirmed_at,
62 | 'administrator'=> $this->administrator,
63 | 'created_at' => $this->created_at,
64 | 'updated_at' => $this->updated_at,
65 | ]);
66 |
67 | $query->andFilterWhere(['like', 'login', $this->login]);
68 |
69 | return $dataProvider;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/simple/views/manager/_assignment.php:
--------------------------------------------------------------------------------
1 | 'kartik\grid\CheckboxColumn',
10 | 'width' => '20px',
11 | ],
12 | [
13 | 'class' => 'kartik\grid\SerialColumn',
14 | 'width' => '30px',
15 | ],
16 | [
17 | 'class' => '\kartik\grid\DataColumn',
18 | 'attribute' => 'id',
19 | 'width' => '40px',
20 | ],
21 | [
22 | 'class' => '\kartik\grid\DataColumn',
23 | 'attribute' => 'login',
24 | ],
25 | [
26 | 'class' => '\kartik\grid\DataColumn',
27 | 'attribute' => 'created_at',
28 | 'value' => function($model) {
29 | return date('d/m/Y', $model->created_at);
30 | },
31 | 'filter' => DatePicker::widget([
32 | 'model' => $searchModel,
33 | 'attribute' => 'created_at',
34 | 'dateFormat' => 'php:Y-m-d',
35 | 'options' => [
36 | 'class' => 'form-control',
37 | ],
38 | ]),
39 | ],
40 | [
41 | 'class' => '\kartik\grid\DataColumn',
42 | 'width' => '50px',
43 | 'attribute' => 'blocked_at',
44 | 'label'=>'Status',
45 | 'value' => function ($model) {
46 | if ($model->blocked_at !== null) {
47 | return Html::a(Yii::t('user', 'Unblock'), ['toggle-block', 'id' => $model->id], [
48 | 'class' => 'btn btn-xs btn-warning btn-block',
49 | 'role' => 'modal-remote',
50 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
51 | 'data-request-method' => 'post',
52 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
53 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
54 | ]);
55 | } else {
56 | return Html::a(Yii::t('user', 'Block'), ['toggle-block', 'id' => $model->id], [
57 | 'class' => 'btn btn-xs btn-danger btn-block',
58 | 'role' => 'modal-remote',
59 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
60 | 'data-request-method' => 'post',
61 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
62 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
63 | ]);
64 | }
65 | },
66 | 'format' => 'raw',
67 | 'visible' => Yii::$app->user->identity->isAdministrator(),
68 | ],
69 | [
70 | 'class' => '\kartik\grid\DataColumn',
71 | 'width' => '130px',
72 | 'attribute' => 'administrator',
73 | 'value' => function ($model) {
74 | if (!$model->administrator) {
75 | return Html::a(Yii::t('user', 'Set SU'), ['toggle-superuser', 'id' => $model->id], [
76 | 'class' => 'btn btn-xs btn-danger btn-block',
77 | 'role' => 'modal-remote',
78 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
79 | 'data-request-method' => 'post',
80 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
81 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
82 | ]);
83 | } else {
84 | return Html::a(Yii::t('user', 'Remove SU'), ['toggle-superuser', 'id' => $model->id], [
85 | 'class' => 'btn btn-xs btn-info btn-block',
86 | 'role' => 'modal-remote',
87 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
88 | 'data-request-method' => 'post',
89 | 'data-confirm-title' => Yii::t('user', 'Are you sure?'),
90 | 'data-confirm-message' => Yii::t('user', 'Are you sure you want to unblock this user?'),
91 | ]);
92 | }
93 | },
94 | 'format' => 'raw',
95 | 'visible' => Yii::$app->user->identity->isAdministrator(),
96 | 'filter' => [0 => 'Not Admin', 1 => 'Admin'],
97 | ]
98 | ];
99 |
100 | $rbacModule = Yii::$app->getModule('rbac');
101 | if (get_class($rbacModule) === 'johnitvn\rbacplus\Module') {
102 | /**
103 | * Intergrate with Rbac Plus extension
104 | */
105 | $columns[] = [
106 | 'class' => 'kartik\grid\DataColumn',
107 | 'header' => Yii::t('rbac', 'Assignment'),
108 | 'hAlign' => 'center',
109 | 'value'=>function($model){
110 | return Html::a('',
111 | ['/rbac/assignment/assignment', 'id' => $model->id],
112 | [
113 | 'role' => 'modal-remote',
114 | 'title' => Yii::t('user', 'Assignment'),
115 | ]
116 | );
117 | },
118 | 'format' => 'raw',
119 | 'visible' => Yii::$app->user->identity->isAdministrator(),
120 | ];
121 |
122 | }
123 |
124 |
125 | $columns[] = [
126 | 'class' => 'kartik\grid\ActionColumn',
127 | 'dropdown' => false,
128 | 'vAlign' => 'middle',
129 | 'hAlign' => 'center',
130 | 'urlCreator' => function($action, $model, $key, $index) {
131 | return Url::to([$action, 'id' => $key]);
132 | },
133 | 'viewOptions' => ['role' => 'modal-remote', 'title' => 'View', 'data-toggle' => 'tooltip'],
134 | 'updateOptions' => ['role' => 'modal-remote', 'title' => 'Update', 'data-toggle' => 'tooltip'],
135 | 'deleteOptions' => ['role' => 'modal-remote', 'title' => 'Delete',
136 | 'data-confirm' => false, 'data-method' => false, // for overide yii data api
137 | 'data-request-method' => 'post',
138 | 'data-toggle' => 'tooltip',
139 | 'data-confirm-title' => 'Are you sure?',
140 | 'data-confirm-message' => 'Are you sure want to delete this item'],
141 | ];
142 |
143 | return $columns;
--------------------------------------------------------------------------------
/src/simple/views/manager/_detail.php:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 | = DetailView::widget([
9 | 'model' => $model,
10 | 'attributes' => [
11 | 'id',
12 | 'login',
13 | [
14 | 'attribute'=>'administrator',
15 | 'value'=> $model['administrator']?"Yes":"No"
16 | ],
17 | [
18 | 'attribute'=>'creator',
19 | 'format' => 'raw',
20 | 'value'=> $model['creator']==-1?"Created by Console":
21 | $model['creator']==-2?"User register by my self":
22 | Html::a(UserAccounts::findOne($model['creator'])->login,['/user/manager/view','id'=>UserAccounts::findOne($model['creator'])->id],["role"=>"modal-remote"])
23 | ],
24 | 'creator_ip',
25 | [
26 | 'attribute'=>'blocked_at',
27 | 'value'=> $model['blocked_at']==null?"Not blocked":date("d/m/Y H:i:s",$model['blocked_at'])
28 | ],
29 | [
30 | 'attribute'=>'created_at',
31 | 'value'=>date("d/m/Y H:i:s",$model['created_at'])
32 | ],
33 | [
34 | 'attribute'=>'updated_at',
35 | 'value'=>$model['updated_at']==-1?\Yii::t("user","Never Update"):date("d/m/Y H:i:s",$model['updated_at'])
36 | ],
37 | ],
38 | ]) ?>
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/simple/views/manager/_form.php:
--------------------------------------------------------------------------------
1 |
5 |
6 |
26 |
--------------------------------------------------------------------------------
/src/simple/views/manager/create.php:
--------------------------------------------------------------------------------
1 |
2 | = $this->render('_form', [
3 | 'model' => $model,
4 | ]) ?>
5 |
6 |
--------------------------------------------------------------------------------
/src/simple/views/manager/index.php:
--------------------------------------------------------------------------------
1 | title = 'Users';
9 | $this->params['breadcrumbs'][] = $this->title;
10 |
11 | CrudAsset::register($this);
12 |
13 | ?>
14 |
15 |
16 | =GridView::widget([
17 | 'id'=>'crud-datatable',
18 | 'dataProvider' => $dataProvider,
19 | 'filterModel' => $searchModel,
20 | 'pjax'=>true,
21 | 'columns' => require(__DIR__.'/_columns.php'),
22 | 'toolbar'=> [
23 | ['content'=>
24 | Html::a('
', ['create'],
25 | ['role'=>'modal-remote','title'=> 'Create new Users','class'=>'btn btn-default']).
26 | Html::a('
', [''],
27 | ['data-pjax'=>1, 'class'=>'btn btn-default', 'title'=>'Reset Grid']).
28 | '{toggleData}'.
29 | '{export}'
30 | ],
31 | ],
32 | 'striped' => true,
33 | 'condensed' => true,
34 | 'responsive' => true,
35 | 'panel' => [
36 | 'type' => 'primary',
37 | 'heading' => '
Users listing',
38 | 'before'=>'
* Resize table columns just like a spreadsheet by dragging the column edges.',
39 | 'after'=>BulkButtonWidget::widget([
40 | 'buttons'=> Html::a('
Delete All',
41 | ["bulk-delete"] ,
42 | [
43 | "class"=>"btn btn-danger btn-xs",
44 | 'role'=>'modal-remote-bulk',
45 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
46 | 'data-request-method'=>'post',
47 | 'data-confirm-title'=>'Are you sure?',
48 | 'data-confirm-message'=>'Are you sure want to delete all this items'
49 | ]).' '.
50 | Html::a('
Block All',
51 | ["bulk-block"] ,
52 | [
53 | "class"=>"btn btn-danger btn-xs",
54 | 'role'=>'modal-remote-bulk',
55 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
56 | 'data-request-method'=>'post',
57 | 'data-confirm-title'=>'Are you sure?',
58 | 'data-confirm-message'=>'Are you sure want to block all this items'
59 | ]).' '.
60 | Html::a('
Unblock All',
61 | ["bulk-unblock"] ,
62 | [
63 | "class"=>"btn btn-warning btn-xs",
64 | 'role'=>'modal-remote-bulk',
65 | 'data-confirm'=>false, 'data-method'=>false,// for overide yii data api
66 | 'data-request-method'=>'post',
67 | 'data-confirm-title'=>'Are you sure?',
68 | 'data-confirm-message'=>'Are you sure want to unblock all this items'
69 | ]),
70 | ]).
71 | '
',
72 | ]
73 | ])?>
74 |
75 |
76 | "ajaxCrubModal",
78 | "footer"=>"",// always need it for jquery plugin
79 | ])?>
80 |
81 |
--------------------------------------------------------------------------------
/src/simple/views/manager/update.php:
--------------------------------------------------------------------------------
1 |
2 | = $this->render('_form', [
3 | 'model' => $model,
4 | ]) ?>
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/simple/views/manager/view.php:
--------------------------------------------------------------------------------
1 | [
6 | [
7 | 'label' => Yii::t('user','User Detail'),
8 | 'content' => $this->render("_detail",['model'=>$model]),
9 | 'active' => true
10 | ],
11 | [
12 | 'label' => Yii::t('user','Role Assignment'),
13 | 'content' => $this->render("_assignment",['model'=>$model]),
14 | ],
15 | ]
16 | ]);
17 |
--------------------------------------------------------------------------------
/src/simple/views/security/change-password.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Change Password');
5 | ?>
6 |
7 |
8 |
9 |
10 |
= Html::encode($this->title) ?>
11 |
12 |
13 | 'change-password-form',
15 | 'enableAjaxValidation' => true,
16 | 'enableClientValidation' => false,
17 | ]); ?>
18 |
19 | = $form->field($model, 'old_password')->passwordInput() ?>
20 |
21 | = $form->field($model, 'new_password')->passwordInput() ?>
22 |
23 | = $form->field($model, 'confirm_password')->passwordInput() ?>
24 |
25 | = Html::submitButton(Yii::t('user', 'Save'), ['class' => 'btn btn-primary btn-block']) ?>
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/simple/views/security/login.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Sign in');
7 | $module = Yii::$app->getModule('user');
8 | ?>
9 |
10 |
11 |
12 |
13 |
= Html::encode($this->title) ?>
14 |
15 |
16 | 'login-form',
18 | 'enableAjaxValidation' => true,
19 | 'enableClientValidation' => false,
20 | 'validateOnBlur' => false,
21 | 'validateOnType' => false,
22 | 'validateOnChange' => false,
23 | ]) ?>
24 |
25 | = $form->field($model, 'login', ['inputOptions' => ['autofocus' => 'autofocus', 'class' => 'form-control', 'tabindex' => '1']]) ?>
26 |
27 | = $form->field($model, 'password', ['inputOptions' => ['class' => 'form-control', 'tabindex' => '2']])->passwordInput()->label(Yii::t('user', 'Password')) ?>
28 |
29 | = $form->field($model, 'rememberMe')->checkbox(['tabindex' => '4']) ?>
30 |
31 | = Html::submitButton(Yii::t('user', 'Sign in'), ['class' => 'btn btn-primary btn-block', 'tabindex' => '3']) ?>
32 |
33 |
34 |
35 |
36 | enableRegister): ?>
37 |
38 | = Html::a(Yii::t('user', 'Don\'t have an account? Sign up!'), ['/user/security/register']) ?>
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/src/simple/views/security/register.php:
--------------------------------------------------------------------------------
1 | title = Yii::t('user', 'Sign up');
5 | ?>
6 |
7 |
8 |
9 |
10 |
= Html::encode($this->title) ?>
11 |
12 |
13 | 'registration-form',
15 | 'enableAjaxValidation' => true,
16 | 'enableClientValidation' => false,
17 | ]); ?>
18 |
19 | = $form->field($model, 'login') ?>
20 |
21 | = $form->field($model, 'password')->passwordInput() ?>
22 |
23 | = $form->field($model, 'confirm_password')->passwordInput() ?>
24 |
25 | = Html::submitButton(Yii::t('user', 'Sign up'), ['class' => 'btn btn-primary btn-block']) ?>
26 |
27 |
28 |
29 |
30 |
31 | = Html::a(Yii::t('user', 'Already registered? Sign in!'), ['/user/security/login']) ?>
32 |
33 |
34 |
--------------------------------------------------------------------------------