Redbrick User management tool
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.

149 lines
4.5 KiB

  1. #-----------------------------------------------------------------------------#
  2. # MODULE DESCRIPTION #
  3. #-----------------------------------------------------------------------------#
  4. """RedBrick User Module; contains RBUser class."""
  5. #-----------------------------------------------------------------------------#
  6. # DATA #
  7. #-----------------------------------------------------------------------------#
  8. __version__ = '$Revision: 1.4 $'
  9. __author__ = 'Cillian Sharkey'
  10. #-----------------------------------------------------------------------------#
  11. # CLASSES #
  12. #-----------------------------------------------------------------------------#
  13. class RBUser:
  14. """Class to represent a user."""
  15. # List of valid LDAP attributes for a user. The order of these is used
  16. # when displaying a user's information.
  17. #
  18. attr_list = (
  19. # Attributes associated with user.
  20. 'uid', # Username
  21. 'usertype', # XXX NOT IN LDAP: contains primary
  22. # usertype from objectClass list.
  23. # Placed here so it's at start of
  24. # output for user's information.
  25. 'objectClass', # List of classes.
  26. 'newbie', # New this year (boolean)
  27. 'cn', # Full name
  28. 'altmail', # Alternate email
  29. 'id', # DCU ID number (integer)
  30. 'course', # DCU course code
  31. 'year', # DCU course year number/code
  32. 'yearsPaid', # Number of years paid (integer)
  33. 'updatedby', # Username
  34. 'updated', # Timestamp
  35. 'createdby', # Username
  36. 'created', # Timestamp
  37. 'birthday', # Date
  38. # Attributes associated with Unix account.
  39. 'uidNumber',
  40. 'gidNumber',
  41. 'gecos',
  42. 'loginShell',
  43. 'homeDirectory',
  44. 'userPassword', # Crypted password.
  45. 'host', # List of hosts.
  46. 'shadowLastChange' # Last time password was changed.
  47. )
  48. # List of additional user attributes that are NOT in LDAP.
  49. #
  50. attr_misc_list = (
  51. 'passwd', # Plaintext password
  52. 'oldusertype', # Used when converting usertype?
  53. 'bday', # Birthday day
  54. 'bmonth', # Birthday month
  55. 'byear', # Birthday year
  56. 'disuser_period', # at(1) timespec
  57. #XXX remove usr.override
  58. #'override' # Boolean
  59. )
  60. # Union of above lists.
  61. #
  62. attr_list_all = attr_list + attr_misc_list
  63. attr_list_info = (
  64. # Attributes associated with user to be used for the useradm info command
  65. 'uid', # Username
  66. 'usertype', # XXX NOT IN LDAP: contains primary
  67. # usertype from objectClass list.
  68. # Placed here so it's at start of
  69. # output for user's information.
  70. 'newbie', # New this year (boolean)
  71. 'cn', # Full name
  72. 'altmail', # Alternate email
  73. 'id', # DCU ID number (integer)
  74. 'course', # DCU course code
  75. 'year', # DCU course year number/code
  76. 'yearsPaid', # Number of years paid (integer)
  77. 'updatedby', # Username
  78. 'updated', # Timestamp
  79. 'createdby', # Username
  80. 'created', # Timestamp
  81. 'birthday', # Date
  82. # Attributes associated with Unix account.
  83. 'gecos',
  84. 'loginShell',
  85. 'homeDirectory',
  86. )
  87. # List of attributes that have multiple values (i.e. are lists).
  88. #
  89. attr_list_value = (
  90. 'objectClass',
  91. 'host'
  92. )
  93. def __init__(self, usr = None, **attrs):
  94. """Create new RBUser object.
  95. If the optional usr argument is an RBUser object, its
  96. attributes are copied to the new object. Only valid RBUser
  97. attributes (listed in RBUser.attr_list_all) are copied. If any
  98. keywords are given, the new object's attributes are set to
  99. their values accordingly. Keywords override data copied from a
  100. given RBUser object. Any remaining unset attributes are
  101. explicitly set to None or an empty list [] as appropriate, so
  102. that they exist within the object.
  103. """
  104. if isinstance(usr, RBUser):
  105. for i in self.attr_list_all:
  106. setattr(self, i, getattr(usr, i))
  107. for i in self.attr_list_all:
  108. if attrs.has_key(i):
  109. setattr(self, i, attrs[i])
  110. elif not hasattr(self, i):
  111. # XXX set list attributes to empty list [] or None ??
  112. #if i in self.attr_list_value:
  113. # setattr(self, i, [])
  114. #else:
  115. setattr(self, i, None)
  116. def merge(self, usr, override = 0):
  117. """Merge attributes from given RBUser object.
  118. For each valid attribute in the given usr object that has
  119. a value (is not None) set this object (self) to that value
  120. if it is currently None or if override is true.
  121. """
  122. for i in self.attr_list_all:
  123. if hasattr(usr, i) and (getattr(self, i) == None or override) and getattr(usr, i) != None:
  124. setattr(self, i, getattr(usr, i))