def setUpClass(self): get_root_bar._cache = None # Workaround self.bar, _ = Bar.objects.get_or_create(id='barjone') self.wrong_bar, _ = Bar.objects.get_or_create(id='barrouje') self.user, _ = User.objects.get_or_create(username='******') self.account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.user) self.wrong_user, _ = User.objects.get_or_create(username='******') self.wrong_account, _ = Account.objects.get_or_create(bar=self.wrong_bar, owner=self.wrong_user) self.staff_user, _ = User.objects.get_or_create(username='******') self.staff_account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.staff_user) Role.objects.get_or_create(name='staff', bar=self.bar, user=self.staff_user) Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.staff_user) self.staff_user = reload(self.staff_user) self.sellitem, _ = SellItem.objects.get_or_create(bar=self.bar, name="Chocolat", tax=0.2) self.itemdetails, _ = ItemDetails.objects.get_or_create(name="Chocolat") self.buyitem, _ = BuyItem.objects.get_or_create(details=self.itemdetails, itemqty=2.5) self.stockitem, _ = StockItem.objects.get_or_create(bar=self.bar, sellitem=self.sellitem, details=self.itemdetails, price=1) self.sellitem2, _ = SellItem.objects.get_or_create(bar=self.bar, tax=0.1) self.itemdetails2, _ = ItemDetails.objects.get_or_create(name="Pizza") self.buyitem2, _ = BuyItem.objects.get_or_create(details=self.itemdetails2, itemqty=3) self.buyitemprice2, _ = BuyItemPrice.objects.get_or_create(buyitem=self.buyitem2, bar=self.bar, price=2)
def has_perm(self, user, perm, obj=None): if not user.is_authenticated() or not user.is_active: return False if DEBUG: print("Logic: ", perm, obj) bar = get_root_bar() return user.has_perm(perm, bar)
def has_perm(self, user, perm, obj=None): if not user.is_authenticated() or not user.is_active: return False bar_perm = "bar" == perm.split(".")[1].split("_")[1] if isinstance(obj, Bar) and (obj == get_root_bar() or not bar_perm): return _has_perm_in_bar(user, perm, obj) else: return super(PermissionBackend, self).has_perm(user, perm, obj)
def setUpClass(self): get_root_bar._cache = None # Workaround root_bar = get_root_bar() self.manager, _ = User.objects.get_or_create(username="******") Role.objects.get_or_create(bar=root_bar, user=self.manager, name="admin") self.manager = reload(self.manager) # prevent role caching self.bar, _ = Bar.objects.get_or_create(id="barrouje") serializer = BarSerializer(self.bar) self.data = serializer.data self.data['name'] = "barjone" self.bar_url = '/bar/%s/' % self.bar.id
def setUpClass(self): get_root_bar._cache = None # Workaround root_bar = get_root_bar() self.manager, _ = User.objects.get_or_create(username="******") Role.objects.get_or_create(bar=root_bar, user=self.manager, name="admin") self.manager = reload(self.manager) # prevent role caching self.user, _ = User.objects.get_or_create(username="******") self.user.set_password("password") self.user.save() serializer = UserSerializer(self.user) self.data = serializer.data self.user_url = '/user/%d/' % self.user.id
def has_perm(self, user, perm, obj=None): if not user.is_authenticated() or not user.is_active: return False bar_perm = "bar" == perm.split(".")[1].split("_")[1] if isinstance(obj, Bar) and (obj == get_root_bar() or not bar_perm): method = "bar" res = _has_perm_in_bar(user, perm, obj) else: method = "obj" res = super(PermissionBackend, self).has_perm(user, perm, obj) if DEBUG: print("Backend (%s): " % method, perm, repr(obj), res, list(reduce(lambda x, y:x | set(y), [r.get_permissions() for r in user.role_set.all()], set()))) return res
def setUpClass(self): self.bar, _ = Bar.objects.get_or_create(id='natationjone') Bar.objects.get_or_create(id='avironjone') self.user, _ = User.objects.get_or_create(username='******') self.user2, _ = User.objects.get_or_create(username='******') self.root, _ = User.objects.get_or_create(username='******') Role.objects.get_or_create(name='admin', bar=self.bar, user=self.user2) self.user2 = reload(self.user2) Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.root) self.root = reload(self.root) self.create_data = {'user': self.user.id, 'name': 'customer'} self.create_data_root = {'user': self.user.id, 'name': 'usermanager'}
def setUpTestData(self): super(UserTests, self).setUpTestData() get_root_bar._cache = None # Workaround root_bar = get_root_bar() self.manager, _ = User.objects.get_or_create(username="******") Role.objects.get_or_create(bar=root_bar, user=self.manager, name="admin") self.manager = reload(self.manager) # prevent role caching self.user, _ = User.objects.get_or_create(username="******") self.user.set_password("password") self.user.email = "*****@*****.**" self.user.save() serializer = UserSerializer(self.user) self.data = serializer.data self.user_url = '/user/%d/' % self.user.id
def setUpTestData(self): super(RoleTests, self).setUpTestData() get_root_bar._cache = None # Workaround self.bar, _ = Bar.objects.get_or_create(id='natationjone') Bar.objects.get_or_create(id='avironjone') self.user, _ = User.objects.get_or_create(username='******') self.user2, _ = User.objects.get_or_create(username='******') self.root, _ = User.objects.get_or_create(username='******') Role.objects.get_or_create(name='admin', bar=self.bar, user=self.user2) self.user2 = reload(self.user2) Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.root) self.root = reload(self.root) self.create_data = {'user': self.user.id, 'name': 'customer'} self.create_data_root = {'user': self.user.id, 'name': 'usermanager'}
def setUpClass(self): get_root_bar._cache = None # Workaround self.bar, _ = Bar.objects.get_or_create(id='barjone') self.wrong_bar, _ = Bar.objects.get_or_create(id='barrouje') self.user, _ = User.objects.get_or_create(username='******') self.account, _ = Account.objects.get_or_create(bar=self.bar, owner=self.user) self.wrong_user, _ = User.objects.get_or_create(username='******') self.wrong_account, _ = Account.objects.get_or_create( bar=self.wrong_bar, owner=self.wrong_user) self.staff_user, _ = User.objects.get_or_create(username='******') self.staff_account, _ = Account.objects.get_or_create( bar=self.bar, owner=self.staff_user) Role.objects.get_or_create(name='staff', bar=self.bar, user=self.staff_user) Role.objects.get_or_create(name='admin', bar=get_root_bar(), user=self.staff_user) self.staff_user = reload(self.staff_user) self.sellitem, _ = SellItem.objects.get_or_create(bar=self.bar, name="Chocolat", tax=0.2) self.itemdetails, _ = ItemDetails.objects.get_or_create( name="Chocolat") self.buyitem, _ = BuyItem.objects.get_or_create( details=self.itemdetails, itemqty=2.5) self.stockitem, _ = StockItem.objects.get_or_create( bar=self.bar, sellitem=self.sellitem, details=self.itemdetails, price=1) self.sellitem2, _ = SellItem.objects.get_or_create(bar=self.bar, tax=0.1) self.itemdetails2, _ = ItemDetails.objects.get_or_create(name="Pizza") self.buyitem2, _ = BuyItem.objects.get_or_create( details=self.itemdetails2, itemqty=3) self.buyitemprice2, _ = BuyItemPrice.objects.get_or_create( buyitem=self.buyitem2, bar=self.bar, price=2)
def has_perm(self, user, perm, obj=None): if not user.is_authenticated() or not user.is_active: return False bar_perm = "bar" == perm.split(".")[1].split("_")[1] if isinstance(obj, Bar) and (obj == get_root_bar() or not bar_perm): method = "bar" res = _has_perm_in_bar(user, perm, obj) else: method = "obj" res = super(PermissionBackend, self).has_perm(user, perm, obj) if DEBUG: print( "Backend (%s): " % method, perm, repr(obj), res, list( reduce(lambda x, y: x | set(y), [r.get_permissions() for r in user.role_set.all()], set()))) return res
def has_permission(self, perm_obj, request, view): bar = get_root_bar() return self.has_object_permission(perm_obj, request, view, bar)
def has_perm(self, user, perm, obj=None): if not user.is_authenticated() or not user.is_active: return False bar = get_root_bar() return user.has_perm(perm, bar)
def get_permissions(self): if self.bar == get_root_bar(): rmap = root_roles_map else: rmap = roles_map return sorted(set(rmap[self.name])) if self.name in rmap else []