Skip to content

Commit

Permalink
add comment for the role v2
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Sep 6, 2024
1 parent e23a08d commit 7c305d8
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions src/Http/Controllers/Api/V2/Admin/User/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

class RoleController extends Controller
{

/**
* Role list
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
* @throws \Exception
*
*/
public function index(Request $request)
{
$validator = Validator::make($request->all(), [
Expand All @@ -19,6 +28,12 @@ public function index(Request $request)
'name' => 'nullable|string',
'value' => 'nullable|string',
'status' => 'nullable|integer',
], [
'page.required' => 'page is required',
'pageSize.required' => 'pageSize is required',
'page.integer' => 'page must be an integer',
'pageSize.integer' => 'pageSize must be an integer',
'page.min' => 'page must be greater than 0',
]);
if ($validator->fails())
{
Expand Down Expand Up @@ -59,6 +74,13 @@ public function index(Request $request)
return $this->success('success', $result);
}

/**
* Role detail
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
* @throws \Exception
*/
public function update(Request $request)
{
$validator = Validator::make($request->all(), [
Expand All @@ -68,6 +90,12 @@ public function update(Request $request)
'desc' => 'nullable|string',
'status' => 'required|integer',
'permissions' => 'array',
], [
'permissions.array' =>'permissions must be an array',
'id.required' => 'id is required',
'name.required' => 'name is required',
'value.required' => 'value is required',
'status.required' => 'status is required',
]);
if ($validator->fails())
{
Expand All @@ -81,9 +109,9 @@ public function update(Request $request)
DB::beginTransaction();
try
{
// 删除角色所有权限
// delete role permissions
Enforcer::deletePermissionsForUser($request->value);
// 添加角色权限
// add role permissions
$permissions = Permission::whereIn('id', $request->permissions)
->get([
'id',
Expand All @@ -108,11 +136,21 @@ public function update(Request $request)
return $this->success(trans('Success'), []);
}

/**
* Role set status
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
* @throws \Exception
*/
public function setStatus(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required|integer',
'status' => 'required|integer',
], [
'id.required' => 'id is required',
'status.required' => 'status is required',
]);
if ($validator->fails())
{
Expand All @@ -128,13 +166,24 @@ public function setStatus(Request $request)
return $this->success(trans('Success'), []);
}

/**
* Role create
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
* @throws \Exception
*/
public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string',
'value' => 'required|string',
'desc' => 'nullable|string',
'status' => 'required|integer',
], [
'name.required' => 'name is required',
'value.required' => 'value is required',
'status.required' => 'status is required',
]);
if ($validator->fails())
{
Expand All @@ -143,9 +192,9 @@ public function create(Request $request)
DB::beginTransaction();
try
{
// 添加角色
// add role
Enforcer::addRoleForUser($request->name, $request->value);
// 添加角色权限
// add role permissions
$permissions = Permission::whereIn('id', $request->permissions)
->get([
'id',
Expand All @@ -172,10 +221,19 @@ public function create(Request $request)
return $this->success(trans('Success'), []);
}

/**
* Role delete
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
* @throws \Exception
*/
public function delete(Request $request)
{
$validator = Validator::make($request->all(), [
'id' => 'required|integer',
], [
'id.required' => 'id is required',
]);
if ($validator->fails())
{
Expand Down

0 comments on commit 7c305d8

Please sign in to comment.