AuthController.php 1.89 KB
<?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);
    }
}