old gnuviechadmin web interface code that is not developed anymore
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.6KB

  1. # -*- coding: utf-8 -*-
  2. from authkit.users import Users
  3. from gnuviechadminweb.model.user import Group, Role, User
  4. from paste.util.import_string import eval_import
  5. import logging
  6. log = logging.getLogger(__name__)
  7. def needsConnection(func):
  8. def wrapper(*__args, **__kw):
  9. from sqlalchemy.orm import create_session
  10. engine = __args[0].meta.engine
  11. conn = engine.contextual_connect()
  12. if conn.closed:
  13. conn = engine.connect()
  14. __args[0].session = create_session(bind=conn)
  15. try:
  16. return func(*__args, **__kw)
  17. finally:
  18. conn.close()
  19. else:
  20. __args[0].session = create_session(bind=conn)
  21. return func(*__args, **__kw)
  22. return wrapper
  23. class GVAUsers(Users):
  24. def __init__(self, data, encrypt = None):
  25. Users.__init__(self, data, encrypt)
  26. log.debug("in __init__")
  27. self.meta = eval_import(self.data)
  28. self.session = self.meta.Session
  29. @needsConnection
  30. def _getSession(self):
  31. return self.session
  32. def _get_group(self, groupname):
  33. return self._getSession().query(Group).filter_by(name=groupname).one()
  34. def _get_user(self, username):
  35. return self._getSession().query(User).filter_by(name=username).one()
  36. def _get_role(self, rolename):
  37. return self._getSession().query(Role).filter_by(name=rolename).one()
  38. # Create Methods
  39. def user_create(self, username, password, group=None):
  40. n_user = User()
  41. n_user.name = username
  42. n_user.password = self.encrypt(password)
  43. if group:
  44. n_user.group = self._get_group(group)
  45. self._getSession().save(n_user)
  46. self._getSession().commit()
  47. def role_create(self, role):
  48. n_role = Role()
  49. n_role.name = role
  50. self._getSession().save(n_role)
  51. self._getSession().commit()
  52. def group_create(self, group):
  53. n_group = Group()
  54. n_group.name = group
  55. self._getSession().save(n_group)
  56. self._getSession().commit()
  57. # Delete Methods
  58. def user_delete(self, username):
  59. self._getSession().delete(self._get_user(username))
  60. self._getSession().commit()
  61. def role_delete(self, role):
  62. self._getSession().delete(self._get_role(role))
  63. self._getSession().commit()
  64. def group_delete(self, group):
  65. self._getSession().delete(self._get_group())
  66. self._getSession().commit()
  67. # Delete Cascade Methods
  68. def role_delete_cascade(self, role):
  69. n_role = self._get_role(role)
  70. for user in self._getSession().query(User).roles.any(name=role).all():
  71. del user.roles[n_role.id]
  72. self._getSession().delete(n_role)
  73. self._getSession().commit()
  74. def group_delete_cascade(self, group):
  75. n_group = self._get_group(group)
  76. self._getSession().delete(self._getSession().query(User).filter_by(
  77. User.group==n_group))
  78. self._getSession().delete(n_group)
  79. self._getSession().commit()
  80. # Existence Methods
  81. def user_exists(self, username):
  82. return self._getSession().query(User).filter_by(
  83. name=username).count() == 1
  84. def role_exists(self, role):
  85. return self._getSession().query(Role).filter_by(
  86. name=role).count() == 1
  87. def group_exists(self, group):
  88. return self._getSession().query(Group).filter_by(
  89. name=group).count() == 1
  90. # List Methods
  91. def list_roles(self):
  92. return [role.name.lower() for role in self._getSession().query(
  93. Role).all()]
  94. def list_users(self):
  95. return [user.name.lower() for user in self._getSession().query(
  96. User).all()]
  97. def list_groups(self):
  98. return [group.name.lower() for group in self._getSession().query(
  99. Group).all()]
  100. # User Methods
  101. def user(self, username):
  102. user = self._get_user(username)
  103. roles = [role.name.lower() for role in user.roles]
  104. roles.sort()
  105. return {
  106. 'username' : user.name,
  107. 'group' : None if user.group is None else user.group.name,
  108. 'password' : user.password,
  109. 'roles' : roles
  110. }
  111. def user_roles(self, username):
  112. user = self._get_user(username)
  113. roles = [role.name.lower() for role in user.roles]
  114. roles.sort()
  115. return roles
  116. def user_group(self, username):
  117. user = self._get_user(username)
  118. return None if user.group is None else user.group.name
  119. def user_password(self, username):
  120. user = self._get_user(username)
  121. return user.password
  122. def user_has_role(self, username, role):
  123. user = self._get_user(username)
  124. return role in [role.name for role in user.roles]
  125. def user_has_group(self, username, group):
  126. user = self._get_user(username)
  127. return (group is None and user.group is None) or \
  128. (group is not None and user.group is not None and \
  129. group == user.group.name)
  130. def user_has_password(self, username, password):
  131. user = self._get_user(username)
  132. return user.password == self.encrypt(password)
  133. def user_set_username(self, username, new_username):
  134. user = self._get_user(username)
  135. user.name = new_username
  136. self._getSession().update(user)
  137. self._getSession().commit()
  138. def user_set_group(self, username, group, add_if_necessary=False):
  139. if add_if_necessary and self._getSession().query(Group).filter_by(
  140. name=group).count() == 0:
  141. self.group_create(group)
  142. groupobj = self._get_group(group)
  143. user = self._get_user(user)
  144. user.group = groupobj
  145. self._getSession().update(user)
  146. self._getSession().commit()
  147. def user_add_role(self, username, role, add_if_necessary=False):
  148. if add_if_necessary and self._getSession().query(Role).filter_by(
  149. name=role).count() == 0:
  150. self.role_create(role)
  151. roleobj = self._get_role(role)
  152. user = self._get_user(username)
  153. if not roleobj in user.roles:
  154. user.roles.append(roleobj)
  155. self._getSession().update(user)
  156. self._getSession().commit()
  157. def user_remove_role(self, username, role):
  158. roleobj = self._get_role(role)
  159. user = self._get_user(username)
  160. if roleobj in user.roles:
  161. del user.roles[roleobj.id]
  162. self._getSession().commit()
  163. def user_remove_group(self, username):
  164. user = self._get_user(username)
  165. user.group = None
  166. self._getSession().update(user)
  167. self._getSession().commit()