dongzhan9100 2018-05-17 17:42
浏览 35
已采纳

如何使用三个级别的用户访问来路由多级用户列表和创建操作。

Here is the requirements. I will have three levels of access to a dashboard that is restricted from the public (System Admin, Manager, Employee).

System admin, to simply put is essentially super admin. They have the ability to index,show,create,update,delete all companies and users.

Managers are created by system admin and assigned to a company, they have the ability to index,show,create,update,delete employees only for the company they belong to.

Employees have a read only access to their companies information and files.

Here is my current routes for the users. (using Slim Framework)

System Admin only access protected by middleware: These actions are for the system admin to manage all users.

/*** USERS ***/

// View to List Users
$this->get('/users', 'App\Controller\User\UserController:index')->setName('user.index');

// View containing user registration form
$this->get('/users/new', 'App\Controller\User\UserController:create')->setName('user.create');

// Creates new user from registration form
$this->post('/users' , 'App\Controller\User\UserController:store');

// Show single user view
$this->get('/users/{id}', 'App\Controller\User\UserController:show')->setName('user.show');

// View containing edit user form
$this->get('/users/{id}/edit', 'App\Controller\User\UserController:edit')->setName('user.edit');

// Updates new user from edit user form
$this->put('/users/{id}', 'App\Controller\User\UserController:update');

// Deletes new user from edit user form
$this->delete('/users/{id}', 'App\Controller\User\UserController:delete');

System Admin & Manager only access protected by middleware: These interactions are for the manager to manage employees

/*** COMPANIES Employees ***/

// View to List Companies employees
$this->get('/companies/{id}/users', 'App\Controller\Company\CompanyUsersController:index')->setName('company.user.index');

// View containing new employee registration form
$this->get('/companies/{id}/users/new', 'App\Controller\Company\CompanyUsersController:create')->setName('company.user.create');

// Creates new employee from registration form
$this->post('/companies/{id}/users' , 'App\Controller\Company\CompanyUsersController:store');

// Show single user
$this->get('/companies/{id}/users/{id}', 'App\Controller\Company\CompanyUsersController:show')->setName('company.user.show');

// View containing edit company employee form
$this->get('/companies/{id}/users/{id}/edit', 'App\Controller\Company\CompanyUsersController:edit')->setName('company.user.edit');

// Updates user from edit user form
$this->put('/companies/{id}/users/{id}', 'App\Controller\Company\CompanyUsersController:update');

// Deletes new user from edit user form
$this->delete('/companies/{id}/users/{id}', 'App\Controller\Company\CompanyUsersController:delete');

You will notice that for system admin, company id is not required. However, for store manager it is. So these interactions will have separate queries. My question is more about best practice. This solution seems a little redundent to me but it effectively separates concerns as well as simplifies the methods on each controller as well as the middlewares needed for access control. Am I going about this completely wrong?

  • 写回答

1条回答 默认 最新

  • douxia9826 2018-05-29 10:37
    关注

    I would have the same routes for all roles as the URL represents the canonical way to view that information.

    e.g.

    $this->get('/companies/{id}/users', 'App\Controller\Company\CompanyUsersController:index')->setName('company.user.index');
    

    is the correct URL for the list of users for a given company.

    I understand that the store manager can only access users for her own company, but that doesn't mean that the URL has to be different. It means that even though she can access /companies/123/users, she cannot access /companies/456/users. This should be done either in group middleware or in each controller. Personally, I suspect that I'd use group middleware like this:

    $app->group(/companies/{id}, function () {
        $this->get('/users', 'App\Controller\Company\CompanyUsersController:index')->setName('company.user.index');
    })->add(CheckUserCompanyMiddlerware::class);
    

    and either display an error page or redirect back to the correct URL for the store manager if they go to the "wrong" company.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值