我建議在數據庫中創建一系列用戶組,每個用戶組都有一個或多個用戶帳戶級別,然後將整數作爲分層值分配給組,然後對個人帳戶進行相同操作組內的水平,這樣的事情(這是一個關係結構,使用InnoDB):
table: account_groups (Broader account groupings)
Fields:
-id_key - primary key, auto number
-group - unique index
-parent - index, foreign key=account_groups.group (this allows you to create group trees, so you can specify that a county group belongs to a state, and a municipality belongs to a county group, etc.)
-group_hierarchy - integer (0 is highest permission group, each subsequent one step lower)
table: account_levels (Account levels within a group)
Fields:
-id_key - primary key, auto number
-account_level - unique index
-group - index, foreign key=account_groups.group
-account_heirarchy - integer (same as other table but denotes heirarchy within the group
table: user_accounts (Individual user accounts)
Fields:
-id_key - primary key, auto number
-account_id - unique index, user account name
-account_level - index, foreign key=account_levels.account_level
table: user_groups (denotes which tree(s) the user has access to)
Fields:
-id_key - primary key, auto number
-account_id - index, foreign key=user_accounts.account_id
-group - index, foreign key=account_groups.group
再來說權限:
table: permissions (directory of permissions that could be applied)
Fields:
-id_key - primary key, auto number
-permission - unique index, permission identifier
-other stuff you need associated with the individual permissions, based on how you want them to hook into your program
table: permissions_group_permissions (permissions applied at group level)
Fields:
-id_key - primary key, auto number
-group - index, foreign key=account_groups.group
-permission - index, foreign key= permissions.permission
table: permissions_account_permissions (permissions applied at account level)
Fields:
-id_key - primary key, auto number
-account_type - index, foreign key=account_levels.account_level
-permission - index, foreign key=permissions.permission
table: permissions_individual_permissions (permissions applied to individual accounts, if neccessary)
Fields:
-id_key - primary key, auto number
-account_id - index, foreign key=user_accounts.account_id
-permission - index, foreign key=permissions.permission
-allow_or_deny - boolean (TRUE means permission is granted, FALSE means permission if revoked. This allows you to fine tune individual accounts, either granting custom elevated permissions, or revoking individual permissions for troublesome accounts without demoting them from the group. This can be useful in some special circumstances)
-expiration - timestamp (allows you to set expiration dates for permissions, like if you want to temporarily suspend a specific action. Programmatically set default value of 00/00/00 00:00:00 as indefinite. You can do this at the account and group levels too by adding this field to those tables.)
然後可以使用PHP通過對權限進行迭代由fi個人帳戶首先獲取與帳戶級別相關聯的組,然後按分層次序對每個後續組進行排列,然後從當前組中的當前帳戶級別迭代當前組的層級順序(作爲多維陣列添加到組數組)到組內最後一個現有帳戶級別。接下來,您將獲取每個後續組的所有帳戶級別,最後爲已添加到陣列的每個帳戶級別獲取所有關聯權限。如果您實現了單獨的用戶權限,那麼您需要在權限數組中添加單獨應用的權限,最後從陣列中刪除allow_or_deny字段設置爲FALSE的任何權限。如果用戶需要訪問多個樹,則可以向account_groups表中添加一條記錄,以匹配其帳戶ID,表示他們訪問的樹的最高級別是什麼,然後遍歷樹中所有後續組。要向該帳戶授予所有適用的權限,請從user_groups獲取account_id的所有組關聯,然後爲每個樹運行先前描述的過程。如果他們只能訪問一棵樹,則甚至不需要使用user_groups表。
an example of how the structure fits your model:
group: USA, hierarchy = 0
group: California, parent-> USA, hierarchy = 1
group: Los Angeles, parent->California, hierarchy = 2
group: Texas, parent->USA, hierarchy = 1
group: Dallas, parent->Texas, hierarchy = 2
美國組的成員可以訪問所有內容。加州的成員可以訪問所有後續組在加利福尼亞州的層次結構,而不是團體得克薩斯州,即使它們具有相同層次值(因爲它們是不同的家長分支機構)
account levels:
admin, hierarchy=0
manager, hierarchy=1
analyst, hierarchy=2
staff member, hierarchy=3
每個帳戶級別都有所有的每個後續帳戶級別的權限。
user accounts:
Bob, manager (likes to spam junk email to everyone)
您仍然可以通過將電子郵件權限permissions_individual_permissions並設置allow_or_deny值設置爲FALSE撤銷鮑勃電子郵件的權限。這可以讓您阻止Bob發送垃圾郵件,而不會將他從管理中降級。
example PHP array:
$account=array(
groups=>array(), //Step 1: array_push each group the account is a member of here. Repeat for each tree from user_groups.
account_levels=>array(), //Step 2: loop through $account[groups], array_push each level here
permissions=>array(), //Step 3: loop through $account[account_levels], array_push each permission here. Then do the same for individual permissions applied to the account
restrictions=>array() //Step 4: loop through individual permissions where allow_or_deny=FALSE, array_push here (do the same for group and account level if you implemented restrictions for those tables as well). Tell your program to ignore permissions from this array, even if the account would otherwise have them.
);
此外,這將允許您爲不同的樹設置不同的權限級別,因此單個用戶可以在一棵樹中訪問狀態,但只能在另一棵樹中訪問市級。 – mopsyd