-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plugin.php
109 lines (92 loc) · 3.82 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace Tlokuus\DisablePassword;
use Auth;
use Event;
use Lang;
use Str;
use October\Rain\Auth\AuthException;
use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use RainLab\User\Components\Account as AccountComponent;
class Plugin extends PluginBase
{
public $require = ['RainLab.User'];
public function pluginDetails()
{
return [
'name' => 'Disable Password Auth',
'description' => 'Disable auth with password for specific users, forcing the user to choose another auth method.',
'author' => 'Tlokuus',
'icon' => 'icon-users'
];
}
public function boot()
{
/*
* Display a message when user with disabled password try to log in
*/
Event::listen('rainlab.user.beforeAuthenticate', function($component, $credentials){
if(!array_key_exists('password', $credentials)){
return;
}
$user = Auth::findUserByLogin($credentials['login']);
if($user && $user->tlokuus_disablepassword_is_disabled){
$message = Lang::get('tlokuus.disablepassword::lang.password_disabled');
Event::fire('auth.user_without_password_login_attempt', [$user, &$message]);
throw new AuthException($message);
}
});
/*
* Add "Mark password as unset" field on backend
*/
Event::listen('backend.form.extendFields', function($widget) {
if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
return;
}
if (!$widget->model instanceof \RainLab\User\Models\User) {
return;
}
if ($widget->getContext() != 'update') {
return;
}
$widget->addTabFields([
'tlokuus_disablepassword_is_disabled' => [
'label' => 'Mark password as unset',
'tab' => 'rainlab.user::lang.user.account',
'comment' => 'If option enabled, will disable ability for user to login with a password until a new password is set.',
'type' => 'checkbox'
]
]);
});
/*
* React to User model changes
*/
UserModel::extend(function($model){
$model->addFillable('tlokuus_disablepassword_is_disabled');
$model->bindEvent('model.saveInternal', function() use ($model){
if(array_key_exists('tlokuus_disablepassword_is_disabled', $model->getDirty()) && $model->tlokuus_disablepassword_is_disabled){
// Simulate an unset password by generating a random one
$model->password_confirmation = $model->password = Str::random(40);
}elseif(array_key_exists('password', $model->getDirty())){
// Once password is changed, remove the unset flag.
$model->tlokuus_disablepassword_is_disabled = false;
}
}, 600); // Validation priority is 500. We must perform changes before validation.
});
/*
* Do not ask for password when changing account details if no password is set
*/
Event::listen('cms.page.initComponents', function ($controller, $page, $layout) {
$user = Auth::getUser();
if(!$user || $user->is_guest){
return;
}
foreach($page->components as $comp){
if($comp instanceof AccountComponent){
$requirePassword = $comp->property('requirePassword', false) && !$user->tlokuus_disablepassword_is_disabled;
$comp->setProperty('requirePassword', $requirePassword);
}
}
});
}
}