在我的django - app中,我使用以下组为不同的facilities
设置权限:
ADMIN_<facility>
USER_<facility>
admin拥有用户拥有的所有权限以及更多权限。
我想约束具有组ADMIN_facility1
的用户永远不会添加到组USER_facility1
。我的方法:
class MyUser(models.Model):
...
@property
def admin_facilities(self):
[g.name for g in self.groups.filter(name__startswith="ADMIN_")]
@property
def user_facilities(self):
[g.name for g in self.groups.filter(name__startswith="USER_")]
def save(self, **kwargs):
for user_fac in self.user_facilities:
if user_fac in self.admin_facilities:
# remove the user facility
self.groups.remove(
self.groups.get(name="USER_{}".format(user_fac)
)
super(MyUser, self).save(**kwargs)
...
这种方法不起作用,因为在调用保存函数期间组尚不存在。
问题:如何约束ADMIN_<facility>
组中的用户永远不能添加到相应的USER_<facility>
组?
答案 0 :(得分:0)
您可以将保存电话发送到开头,如下所示:
def save(self, **kwargs):
super(MyUser, self).save(**kwargs)
for user_fac in self.user_facilities:
if user_fac in self.admin_facilities:
# remove the user facility
self.groups.remove(
self.groups.get(name="USER_{}".format(user_fac)
)
答案 1 :(得分:0)