| Adam Israel | 1a15d1c | 2017-10-23 12:00:49 -0400 | [diff] [blame] | 1 | import logging |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 2 | |
| 3 | import pyrfc3339 |
| Adam Israel | 1a15d1c | 2017-10-23 12:00:49 -0400 | [diff] [blame] | 4 | |
| 5 | from . import tag |
| 6 | |
| 7 | log = logging.getLogger(__name__) |
| 8 | |
| 9 | |
| 10 | class User(object): |
| 11 | def __init__(self, controller, user_info): |
| 12 | self.controller = controller |
| 13 | self._user_info = user_info |
| 14 | |
| 15 | @property |
| 16 | def tag(self): |
| 17 | return tag.user(self.username) |
| 18 | |
| 19 | @property |
| 20 | def username(self): |
| 21 | return self._user_info.username |
| 22 | |
| 23 | @property |
| 24 | def display_name(self): |
| 25 | return self._user_info.display_name |
| 26 | |
| 27 | @property |
| 28 | def last_connection(self): |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 29 | return pyrfc3339.parse(self._user_info.last_connection) |
| Adam Israel | 1a15d1c | 2017-10-23 12:00:49 -0400 | [diff] [blame] | 30 | |
| 31 | @property |
| 32 | def access(self): |
| 33 | return self._user_info.access |
| 34 | |
| 35 | @property |
| 36 | def date_created(self): |
| 37 | return self._user_info.date_created |
| 38 | |
| 39 | @property |
| 40 | def enabled(self): |
| 41 | return not self._user_info.disabled |
| 42 | |
| 43 | @property |
| 44 | def disabled(self): |
| 45 | return self._user_info.disabled |
| 46 | |
| 47 | @property |
| 48 | def created_by(self): |
| 49 | return self._user_info.created_by |
| 50 | |
| 51 | async def set_password(self, password): |
| 52 | """Update this user's password. |
| 53 | """ |
| 54 | await self.controller.change_user_password(self.username, password) |
| 55 | self._user_info.password = password |
| 56 | |
| 57 | async def grant(self, acl='login'): |
| 58 | """Set access level of this user on the controller. |
| 59 | |
| 60 | :param str acl: Access control ('login', 'add-model', or 'superuser') |
| 61 | """ |
| Adam Israel | c3e6c2e | 2018-03-01 09:31:50 -0500 | [diff] [blame] | 62 | if await self.controller.grant(self.username, acl): |
| 63 | self._user_info.access = acl |
| Adam Israel | 1a15d1c | 2017-10-23 12:00:49 -0400 | [diff] [blame] | 64 | |
| 65 | async def revoke(self): |
| 66 | """Removes all access rights for this user from the controller. |
| 67 | """ |
| 68 | await self.controller.revoke(self.username) |
| 69 | self._user_info.access = '' |
| 70 | |
| 71 | async def disable(self): |
| 72 | """Disable this user. |
| 73 | """ |
| 74 | await self.controller.disable_user(self.username) |
| 75 | self._user_info.disabled = True |
| 76 | |
| 77 | async def enable(self): |
| 78 | """Re-enable this user. |
| 79 | """ |
| 80 | await self.controller.enable_user(self.username) |
| 81 | self._user_info.disabled = False |