AuthController.php
1.89 KB
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
<?php
namespace App\Http\Container\AdminSection\Controllers;
use App\Http\Request\UserChangePwdRequest;
use App\Models\SystemPermission;
use App\Models\SystemRole;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function show(Request $request): Response
{
if (Auth::user()?->getAttribute('is_super') === 1) {
$permissions = SystemPermission::query()->pluck('system_permissions.name');
} else {
$roleIds = SystemRole::query()
->join('user_has_roles', 'user_has_roles.role_id', 'system_roles.id')
->where('user_has_roles.user_id', Auth::id())
->where('system_roles.status', 1)
->pluck('system_roles.id')->toArray();
$permissions = SystemPermission::query()
->join('system_role_has_permissions', 'system_role_has_permissions.permission_id', 'system_permissions.id')
->where('system_permissions.guard', 'Admin')
->whereIn('system_role_has_permissions.role_id', $roleIds)
->pluck('system_permissions.name');
}
return $this->successWithData([
'user' => $request->user()->only(['id', 'nick_name', 'avatar', 'role', 'last_login']),
'permissions' => $permissions
]);
}
/**
* @param \App\Http\Request\UserChangePwdRequest $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changePwd(UserChangePwdRequest $request): Response
{
$request->user()->update($request->safe()->toArray());
return $this->success(ApiCode::UPDATE_SUCCESS);
}
}