vd/app/Domains/Permission/Http/Controllers/RoleController.php
2019-01-16 17:38:06 +08:00

135 lines
2.9 KiB
PHP

<?php
namespace App\Domains\Permission\Http\Controllers;
use App\Core\Controller;
use Illuminate\Http\Request;
use App\Exceptions\NotExistException;
use App\Domains\Permission\Services\RoleService;
use App\Domains\Permission\Repositories\RoleRepository;
class RoleController extends Controller
{
protected $request;
protected $roleService;
/**
* 构造函数,自动注入.
*/
public function __construct(Request $request, RoleService $roleService)
{
$this->request = $request;
$this->roleService = $roleService;
$this->account = app('dipper')->initAccount();
}
/**
* 列表.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$conditions = [];
$conditions['limit'] = $this->request->get('limit', 20);
$roles = $this->roleService->index($conditions);
return res($roles, '角色列表', 201);
}
/**
* 详情.
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$role = app(RoleRepository::class)->find($id);
if (!$role) {
throw new NotExistException('角色不存在或已删除');
}
$role->setRelations([
'parent' => $role->parent ? $role->parent->name : '',
'permissions' => $role->permissions->toTree(),
]);
return res($role, '角色详情', 201);
}
/**
* 创建.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$attributes = $this->request->all();
$role = $this->roleService->store($attributes);
return res($role, '创建成功');
}
/**
* 编辑.
*
* @return \Illuminate\Http\Response
*/
public function update($id)
{
$attributes = $this->request->all();
$attributes['id'] = $id;
$role = $this->roleService->store($attributes);
return res($role, '修改成功');
}
/**
* 删除.
*
* @return \Illuminate\Http\Response
*/
public function destroy()
{
$ids = $this->request->ids();
$this->roleService->destroy($ids);
return res(true, '删除成功');
}
/**
* 给账号分配角色
*
* @return void
*/
public function syncRoles()
{
$accountId = $this->request->get('accountid');
$roles = $this->request->ids('role_ids');
$this->roleService->syncRoles($accountId, $roles);
return res(true, '分配成功');
}
/**
* 分配权限
*
* @return void
*/
public function syncPermissions()
{
$roleId = $this->request->get('role_id');
$permissions = $this->request->ids('permission_ids');
$this->roleService->syncPermissions($roleId, $permissions);
return res(true, '分配成功');
}
}