Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet120/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   116   117   118   119   120   121   122   123   ...   183
Bog'liq
python3-oop

[
 119 
]
raise PermissionError("Permission Exists")
def permit_user(self, perm_name, username):
'''Grant the given permission to the user'''
try:
perm_set = self.permissions[perm_name]
except KeyError:
raise PermissionError("Permission does not exist")
else:
if username not in self.authenticator.users:
raise InvalidUsername(username)
perm_set.add(username)
The first 
method allows us to create a new permission, unless it already exists, in 
which case an exception is raised. The second allows us to add a username to a 
permission, unless either the permission or the username doesn't yet exist.
We use 
set
instead of 
list
for usernames, so that even if you grant a user 
permission more than once, the nature of sets means the user is only in the set
once. We'll discuss sets further in a later chapter.

PermissionError
is raised in both methods. This new error doesn't require 
a username, so we'll make it extend 
Exception
directly, instead of our custom 
AuthException
:
class PermissionError(Exception):
pass
Finally, we can add a method to check whether a user has a specific 
permission
or not. In order for them to be granted access, they have to be both logged into the 
authenticator and in the set of people who have been granted access to that privilege. 
If either of these conditions is unsatisfied, an exception is raised:
def check_permission(self, perm_name, username):
if not self.authenticator.is_logged_in(username):
raise NotLoggedInError(username)
try:
perm_set = self.permissions[perm_name]
except KeyError:
raise PermissionError("Permission does not exist")
else:
if username not in perm_set:
raise NotPermittedError(username)
else:
return True
www.it-ebooks.info


Expecting the Unexpected

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   116   117   118   119   120   121   122   123   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish