douzhan2027 2013-11-03 15:28
浏览 95
已采纳

使用PHP + MySQL后端的JS(Angular)前端用户权限

I'm currently building a complex AngularJS based front-end website. The backend is powered with PHP and MySQL.

I have 2 questions, both regarding user privileges.

  1. How will you design the db schema for storing the user privileges? In the future I'll want to add pre-defined privileges (several sets) and use them as free/premium account types.

  2. How will you manage those privileges in the front-end? When will you fetch/re-fetch the privileges? How will you check them on each methods that requires privileges?

I'm looking for a smart solution that won't limit me in the number of privileges I can define but will also provide a default set of basic privileges if nothing is defined.

About the SQL implementation
The privileges should apply on all users in a specific company.
the db schema is something like this:

  • companies (company_id, etc)
  • users (user_id, company_id, etc)

From what I understand the privileges schema should look like this:

  • privileges(prev_id, prev_name)
  • companies_privileges(company_id, prev_id)

User privileges handling
when a user logs in the company privileges are stored with the cookie/session/front-end service.

To verify on the frond-end i use a directive that compares the current user privileges against the required privilege:

data-privilege required=1 user=1

As to the backend part, I'm currently sending a user-token with each request to the server. This token is verified against the database. I can use this to get the user privileges and store them, than check if user can access/use a certain feature.

Besides adding true/false privileges I need support for numerical privileges,
for example: Limit the number of viewable items in a certain list.

Am I thinking right? Am I missing something?

Requirements
- Unlimited privileges per company
- Default privileges should apply on all companies

  • 写回答

2条回答 默认 最新

  • duanhan9479 2013-11-03 16:06
    关注

    A quick answer:

    1. i designed and implemented a sophisticated privilege mechanism that was role-based, verb-per-object, with inheritance. It was rather powerful and was the basis for numerous features in the whole system. Difference between free and premium users was among the requirement, and it got covered perfectly. So this is a direction you may want to follow. It goes without saying that this is server-side logic.

    2. Back then, there was no angular, but generally, in Angular, it is best to do the following: A. have a service that mainly exposes a single call: isAuthorized(privilege-name). B. Have a directive ("needPrivilege='privilege-name') that uses that service and show/hide/disable according to its boolean result. It goes without saying that it uses the isAuthorized. C. Add a logic to the routing that uses that service as well, for limiting access to pages in the UI.

    That's my two cents...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题