-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a PermissionManager core module #116
base: master
Are you sure you want to change the base?
Changes from all commits
1898759
fe3cfc2
f4a4898
6f62e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
import json | ||
import logging | ||
from halibot.message import Message | ||
|
||
def hasPermission(perm, reply=False): | ||
def real_dec(func): | ||
def wrapper(self, msg, *args, **kwargs): | ||
def wrapper(self, *args, **kwargs): | ||
msg = None | ||
for i in list(args) + list(kwargs.values()): | ||
if i.__class__ == Message: | ||
msg = i | ||
break | ||
else: | ||
self.log.error("Probable module bug! -- hasPermission decorator called on a function that doesn't have a Message argument!") | ||
return | ||
|
||
if self._hal.auth.hasPermission(msg.origin, msg.identity, perm): | ||
func(self, msg, *args, **kwargs) | ||
func(self, *args, **kwargs) | ||
elif reply: | ||
self.reply(msg, body="Permission Denied") | ||
return wrapper | ||
|
@@ -47,20 +57,24 @@ def write_perms(self): | |
|
||
def grantPermission(self, ri, identity, perm): | ||
if not self.enabled: | ||
return | ||
return False | ||
|
||
t = (ri, identity, perm) | ||
if t not in self.perms: | ||
self.perms.append(t) | ||
return True | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NB: Should we raise an exception here instead of returning false? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably. I have a lot of return bools in here, most of those should probably be exceptions. Too much C |
||
|
||
def revokePermission(self, ri, identity, perm): | ||
if not self.enabled: | ||
return | ||
return False | ||
|
||
try: | ||
self.perms.remove((ri,identity, perm)) | ||
return True | ||
except Exception as e: | ||
self.log.error("Revocation failed: {}".format(e)) | ||
return False | ||
|
||
def hasPermission(self, ri, identity, perm): | ||
if not self.enabled: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
from .help import Help | ||
from .perm import PermissionManager |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from halibot import CommandModule | ||
from halibot.halauth import hasPermission | ||
|
||
class PermissionManager(CommandModule): | ||
def init(self): | ||
self.commands = { | ||
"grant": self.grant_, | ||
"revoke": self.revoke_, | ||
} | ||
|
||
@hasPermission("PERM_GRANT", reply=True) | ||
def grant_(self, argv, msg=None): | ||
try: | ||
ri, identity, perm = argv.split(" ") | ||
except: | ||
self.reply(msg, body="Must be in the form '<ri> <identity> <perm>'") | ||
return | ||
|
||
if self._hal.auth.grantPermission(ri, identity, perm): | ||
self._hal.auth.write_perms() | ||
|
||
@hasPermission("PERM_REVOKE", reply=True) | ||
def revoke_(self, argv, msg=None): | ||
try: | ||
ri, identity, perm = argv.split(" ") | ||
except: | ||
self.reply(msg, body="Must be in the form '<ri> <identity> <perm>'") | ||
return | ||
|
||
if self._hal.auth.revokePermission(ri, identity, perm): | ||
self._hal.auth.write_perms() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about subclasses of
Message
? I think we should be checking for those too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be checking if the class chain has
Message
.