1
|
|
|
<?php |
2
|
|
|
namespace SimpleCMS\Company\Packages; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\Auth; |
5
|
|
|
use Illuminate\Support\Facades\Hash; |
6
|
|
|
use SimpleCMS\Company\Models\CompanyAccount; |
7
|
|
|
use SimpleCMS\Framework\Exceptions\SimpleException; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class CompanyAuthenticatable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* API 登录 |
15
|
|
|
* @param string $account |
16
|
|
|
* @param string $password |
17
|
|
|
* @param array $messages |
18
|
|
|
* @throws \SimpleCMS\Framework\Exceptions\SimpleException |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function apiLogin(string $account, string $password, array $messages = []): array |
22
|
|
|
{ |
23
|
|
|
$errorMessages = array_merge([ |
24
|
|
|
'not_found' => 'The account or password is incorrect.', |
25
|
|
|
'password_incorrect' => 'The account or password is incorrect.', |
26
|
|
|
'account_invalid' => 'The account has been locked by the system and you can not log on.' |
27
|
|
|
], $messages); |
28
|
|
|
$account = CompanyAccount::where('account', $account)->first(); |
29
|
|
|
if (!$account) { |
30
|
|
|
event('simplecms.plugin.company.login_failed', $account); |
31
|
|
|
throw new SimpleException($errorMessages['not_found']); |
32
|
|
|
} |
33
|
|
|
if (!Hash::check($password, $account->password)) { |
34
|
|
|
event('simplecms.plugin.company.login_failed', $account); |
35
|
|
|
throw new SimpleException($errorMessages['password_incorrect']); |
36
|
|
|
} |
37
|
|
|
if (!$account->is_valid || optional($account->company)->is_valid !== true) { |
38
|
|
|
event('simplecms.plugin.company.login_failed', $account); |
39
|
|
|
throw new SimpleException($errorMessages['account_invalid']); |
40
|
|
|
} |
41
|
|
|
event('simplecms.plugin.company.login_success', $account); |
42
|
|
|
return (array) event('simplecms.plugin.company.api_login', $account); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Guard 登录 |
47
|
|
|
* @param string $guard |
48
|
|
|
* @param string $account |
49
|
|
|
* @param string $password |
50
|
|
|
* @param array $messages |
51
|
|
|
* @throws \SimpleCMS\Framework\Exceptions\SimpleException |
52
|
|
|
* @return bool|RedirectResponse |
53
|
|
|
*/ |
54
|
|
|
public function guardLogin(string $guard, string $account, string $password, array $messages = []): bool|RedirectResponse |
55
|
|
|
{ |
56
|
|
|
$errorMessages = array_merge([ |
57
|
|
|
'not_found' => 'The account or password is incorrect.', |
58
|
|
|
'password_incorrect' => 'The account or password is incorrect.', |
59
|
|
|
'account_invalid' => 'The account has been locked by the system and you can not log on.' |
60
|
|
|
], $messages); |
61
|
|
|
$account = CompanyAccount::where('account', $account)->first(); |
62
|
|
|
if (!$account) { |
63
|
|
|
event('simplecms.plugin.company.login_failed', $account); |
64
|
|
|
return back()->withErrors($errorMessages['not_found']); |
65
|
|
|
} |
66
|
|
|
if (!Auth::guard($guard)->attempt(['account' => $account, 'password' => $password, 'is_valid' => true])) { |
67
|
|
|
return back()->withErrors($errorMessages['password_incorrect']); |
68
|
|
|
} |
69
|
|
|
if (!$account->is_valid || optional($account->company)->is_valid !== true) { |
70
|
|
|
event('simplecms.plugin.company.login_failed', $account); |
71
|
|
|
return back()->withErrors($errorMessages['account_invalid']); |
72
|
|
|
} |
73
|
|
|
event('simplecms.plugin.company.login_success', $account); |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 获取账号基础信息 |
79
|
|
|
* @param \SimpleCMS\Company\Models\CompanyAccount $account |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getAccount(CompanyAccount $account): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'company_name' => optional($account->company)->name, |
86
|
|
|
'company_id' => $account->company_id, |
87
|
|
|
'uid' => $account->uid, |
88
|
|
|
'avatar' => $account->avatar, |
89
|
|
|
'account' => $account->account, |
90
|
|
|
'name' => $account->name, |
91
|
|
|
'roles' => $account->roles, |
92
|
|
|
'is_valid' => $account->is_valid, |
93
|
|
|
'is_founder' => $account->is_founder, |
94
|
|
|
'last_login' => $account->last_login, |
95
|
|
|
'last_ip' => $account->last_ip, |
96
|
|
|
'register_at' => $account->created_at, |
97
|
|
|
'register_ip' => $account->register_ip, |
98
|
|
|
'register_finger' => $account->register_finger, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |