AdminUserController::updateRole()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 3
nop 2
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Date: 2019/2/25 Time: 14:49
4
 *
5
 * @author  Eddy <[email protected]>
6
 * @version v1.0.0
7
 */
8
9
namespace App\Http\Controllers\Admin;
10
11
use App\Http\Controllers\Controller;
12
use App\Http\Requests\Admin\AdminUserRequest;
13
use App\Model\Admin\AdminUser;
14
use App\Repository\Admin\AdminUserRepository;
15
use App\Repository\Admin\RoleRepository;
16
use Illuminate\Database\QueryException;
17
use Illuminate\Http\Request;
18
use Illuminate\Support\Str;
19
20
class AdminUserController extends Controller
21
{
22
    protected $formNames = ['name', 'password', 'status'];
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
28
        $this->breadcrumb[] = ['title' => '管理员管理', 'url' => route('admin::adminUser.index')];
29
    }
30
31
    /**
32
     * 管理员管理-管理员列表
33
     *
34
     */
35
    public function index()
36
    {
37
        $this->breadcrumb[] = ['title' => '管理员列表', 'url' => ''];
38
        return view('admin.adminUser.index', ['breadcrumb' => $this->breadcrumb]);
39
    }
40
41
    /**
42
     * 管理员管理-管理员列表数据
43
     *
44
     * @param Request $request
45
     * @return array
46
     */
47
    public function list(Request $request)
48
    {
49
        $perPage = (int) $request->get('limit', 50);
50
        $this->formNames[] = 'created_at';
51
        $condition = $request->only($this->formNames);
52
        $data = AdminUserRepository::list($perPage, $condition);
53
54
        return $data;
55
    }
56
57
    /**
58
     * 管理员管理-新增管理员
59
     *
60
     */
61
    public function create()
62
    {
63
        $this->breadcrumb[] = ['title' => '新增管理员', 'url' => ''];
64
        return view('admin.adminUser.add', ['breadcrumb' => $this->breadcrumb]);
65
    }
66
67
    /**
68
     * 管理员管理-保存管理员
69
     *
70
     * @param AdminUserRequest $request
71
     * @return array
72
     */
73
    public function save(AdminUserRequest $request)
74
    {
75
        try {
76
            $user = AdminUserRepository::add($request->only($this->formNames));
77
            AdminUserRepository::setDefaultPermission($user);
78
            return [
79
                'code' => 0,
80
                'msg' => '新增成功',
81
                'redirect' => true
82
            ];
83
        } catch (QueryException $e) {
84
            return [
85
                'code' => 1,
86
                'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前用户已存在' : '其它错误'),
87
                'redirect' => false
88
            ];
89
        }
90
    }
91
92
    /**
93
     * 管理员管理-编辑管理员
94
     *
95
     * @param int $id
96
     */
97
    public function edit($id)
98
    {
99
        $this->breadcrumb[] = ['title' => '编辑管理员', 'url' => ''];
100
101
        $user = AdminUserRepository::find($id);
102
        return view('admin.adminUser.add', ['id' => $id, 'user' => $user, 'breadcrumb' => $this->breadcrumb]);
103
    }
104
105
    /**
106
     * 管理员管理-更新管理员
107
     *
108
     * @param AdminUserRequest $request
109
     * @param int $id
110
     * @return array
111
     */
112
    public function update(AdminUserRequest $request, $id)
113
    {
114
        $data = $request->only($this->formNames);
115
        if (!isset($data['status'])) {
116
            $data['status'] = AdminUser::STATUS_DISABLE;
117
        }
118
        if ($request->input('password') == '') {
119
            unset($data['password']);
120
        }
121
122
        try {
123
            AdminUserRepository::update($id, $data);
124
            return [
125
                'code' => 0,
126
                'msg' => '编辑成功',
127
                'redirect' => true
128
            ];
129
        } catch (QueryException $e) {
130
            return [
131
                'code' => 1,
132
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前用户已存在' : '其它错误'),
133
                'redirect' => false
134
            ];
135
        }
136
    }
137
138
    /**
139
     * 管理员管理-分配角色
140
     *
141
     * @param $id
142
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
143
     */
144
    public function role($id)
145
    {
146
        $this->breadcrumb[] = ['title' => '分配角色', 'url' => ''];
147
148
        $roles = RoleRepository::all();
149
        $userRoles = AdminUserRepository::find($id)->getRoleNames();
150
        return view('admin.adminUser.role', [
151
            'id' => $id,
152
            'roles' => $roles,
153
            'breadcrumb' => $this->breadcrumb,
154
            'userRoles' => $userRoles,
155
        ]);
156
    }
157
158
    /**
159
     * 管理员管理-更新用户角色
160
     *
161
     * @param Request $request
162
     * @param $id
163
     * @return array
164
     */
165
    public function updateRole(Request $request, $id)
166
    {
167
        try {
168
            $user = AdminUserRepository::find($id);
169
            $user->syncRoles(array_values($request->input('role')));
170
            return [
171
                'code' => 0,
172
                'msg' => '操作成功',
173
                'redirect' => true
174
            ];
175
        } catch (\Throwable $e) {
176
            return [
177
                'code' => 1,
178
                'msg' => '操作失败',
179
            ];
180
        }
181
    }
182
}
183