我的系统有不同类型的用户:
SuperAdmin,FederationPresident,AssociationPresident,ClubPresident,simpleUser。
关系是:
联盟有多个关联
一个协会有很多俱乐部
俱乐部有很多用户
我使用Laravel,因此我定义了管理用户可以访问和不能访问的策略。
但是,然后,一个FederationUser可以CRUD他联盟中的所有用户,而俱乐部总裁可以只是他俱乐部的CRUD用户。
我最终得到了这样的代码:
Association.php(模型)
public static function fillSelect()
{
$associations = new Collection();
if (Auth::user()->isSuperAdmin()) {
$associations = Association::pluck('name', 'id')->prepend('-', 0);
} else if (Auth::user()->isFederationPresident()) {
$associations = Auth::user()->federationOwned->associations->pluck('name', 'id')->prepend('-', 0);
} else if (Auth::user()->isAssociationPresident()) {
$association = Auth::user()->associationOwned;
$associations->push($association);
$associations = $associations->pluck('name', 'id')->prepend('-', 0);
} else if (Auth::user()->isClubPresident()) {
$association = Auth::user()->clubOwned->association;
$associations->push($association);
$associations = $associations->pluck('name', 'id')->prepend('-', 0);
}
return $associations;
}
Club.php
public static function fillSelect()
{
$clubs = new Collection();
if (Auth::user()->isSuperAdmin()) {
$clubs = Club::pluck('name', 'id');
} else if (Auth::user()->isFederationPresident() && Auth::user()->federationOwned != null) {
$clubs = Auth::user()->federationOwned->clubs;
} else if (Auth::user()->isAssociationPresident() && Auth::user()->associationOwned != null) {
$clubs = Auth::user()->associationOwned->clubs->pluck('name', 'id');
} else if (Auth::user()->isClubPresident() && Auth::user()->clubOwned != null) {
$clubs = Auth::user()->clubOwned->pluck('name', 'id');
}
return $clubs;
}
我觉得这不太好,对于每个模型,我必须做出很多if()。
有没有改进这种东西?