Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

fakepwd.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # -*- test-case-name: twisted.python.test.test_fakepwd -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API.
  6. """
  7. from typing import List
  8. __all__ = ["UserDatabase", "ShadowDatabase"]
  9. class _UserRecord:
  10. """
  11. L{_UserRecord} holds the user data for a single user in L{UserDatabase}.
  12. It corresponds to the C{passwd} structure from the L{pwd} module.
  13. See that module for attribute documentation.
  14. """
  15. def __init__(
  16. self,
  17. name: str,
  18. password: str,
  19. uid: int,
  20. gid: int,
  21. gecos: str,
  22. home: str,
  23. shell: str,
  24. ) -> None:
  25. self.pw_name = name
  26. self.pw_passwd = password
  27. self.pw_uid = uid
  28. self.pw_gid = gid
  29. self.pw_gecos = gecos
  30. self.pw_dir = home
  31. self.pw_shell = shell
  32. def __len__(self) -> int:
  33. return 7
  34. def __getitem__(self, index):
  35. return (
  36. self.pw_name,
  37. self.pw_passwd,
  38. self.pw_uid,
  39. self.pw_gid,
  40. self.pw_gecos,
  41. self.pw_dir,
  42. self.pw_shell,
  43. )[index]
  44. class UserDatabase:
  45. """
  46. L{UserDatabase} holds a traditional POSIX user data in memory and makes it
  47. available via the same API as L{pwd}.
  48. @ivar _users: A C{list} of L{_UserRecord} instances holding all user data
  49. added to this database.
  50. """
  51. _users: List[_UserRecord]
  52. def __init__(self) -> None:
  53. self._users = []
  54. def addUser(
  55. self,
  56. username: str,
  57. password: str,
  58. uid: int,
  59. gid: int,
  60. gecos: str,
  61. home: str,
  62. shell: str,
  63. ) -> None:
  64. """
  65. Add a new user record to this database.
  66. @param username: The value for the C{pw_name} field of the user
  67. record to add.
  68. @param password: The value for the C{pw_passwd} field of the user
  69. record to add.
  70. @param uid: The value for the C{pw_uid} field of the user record to
  71. add.
  72. @param gid: The value for the C{pw_gid} field of the user record to
  73. add.
  74. @param gecos: The value for the C{pw_gecos} field of the user record
  75. to add.
  76. @param home: The value for the C{pw_dir} field of the user record to
  77. add.
  78. @param shell: The value for the C{pw_shell} field of the user record to
  79. add.
  80. """
  81. self._users.append(
  82. _UserRecord(username, password, uid, gid, gecos, home, shell)
  83. )
  84. def getpwuid(self, uid: int) -> _UserRecord:
  85. """
  86. Return the user record corresponding to the given uid.
  87. """
  88. for entry in self._users:
  89. if entry.pw_uid == uid:
  90. return entry
  91. raise KeyError()
  92. def getpwnam(self, name: str) -> _UserRecord:
  93. """
  94. Return the user record corresponding to the given username.
  95. """
  96. if not isinstance(name, str):
  97. raise TypeError(f"getpwuam() argument must be str, not {type(name)}")
  98. for entry in self._users:
  99. if entry.pw_name == name:
  100. return entry
  101. raise KeyError()
  102. def getpwall(self) -> List[_UserRecord]:
  103. """
  104. Return a list of all user records.
  105. """
  106. return self._users
  107. class _ShadowRecord:
  108. """
  109. L{_ShadowRecord} holds the shadow user data for a single user in
  110. L{ShadowDatabase}. It corresponds to C{spwd.struct_spwd}. See that class
  111. for attribute documentation.
  112. """
  113. def __init__(
  114. self,
  115. username: str,
  116. password: str,
  117. lastChange: int,
  118. min: int,
  119. max: int,
  120. warn: int,
  121. inact: int,
  122. expire: int,
  123. flag: int,
  124. ) -> None:
  125. self.sp_nam = username
  126. self.sp_pwd = password
  127. self.sp_lstchg = lastChange
  128. self.sp_min = min
  129. self.sp_max = max
  130. self.sp_warn = warn
  131. self.sp_inact = inact
  132. self.sp_expire = expire
  133. self.sp_flag = flag
  134. def __len__(self) -> int:
  135. return 9
  136. def __getitem__(self, index):
  137. return (
  138. self.sp_nam,
  139. self.sp_pwd,
  140. self.sp_lstchg,
  141. self.sp_min,
  142. self.sp_max,
  143. self.sp_warn,
  144. self.sp_inact,
  145. self.sp_expire,
  146. self.sp_flag,
  147. )[index]
  148. class ShadowDatabase:
  149. """
  150. L{ShadowDatabase} holds a shadow user database in memory and makes it
  151. available via the same API as C{spwd}.
  152. @ivar _users: A C{list} of L{_ShadowRecord} instances holding all user data
  153. added to this database.
  154. @since: 12.0
  155. """
  156. _users: List[_ShadowRecord]
  157. def __init__(self) -> None:
  158. self._users = []
  159. def addUser(
  160. self,
  161. username: str,
  162. password: str,
  163. lastChange: int,
  164. min: int,
  165. max: int,
  166. warn: int,
  167. inact: int,
  168. expire: int,
  169. flag: int,
  170. ) -> None:
  171. """
  172. Add a new user record to this database.
  173. @param username: The value for the C{sp_nam} field of the user record to
  174. add.
  175. @param password: The value for the C{sp_pwd} field of the user record to
  176. add.
  177. @param lastChange: The value for the C{sp_lstchg} field of the user
  178. record to add.
  179. @param min: The value for the C{sp_min} field of the user record to add.
  180. @param max: The value for the C{sp_max} field of the user record to add.
  181. @param warn: The value for the C{sp_warn} field of the user record to
  182. add.
  183. @param inact: The value for the C{sp_inact} field of the user record to
  184. add.
  185. @param expire: The value for the C{sp_expire} field of the user record
  186. to add.
  187. @param flag: The value for the C{sp_flag} field of the user record to
  188. add.
  189. """
  190. self._users.append(
  191. _ShadowRecord(
  192. username, password, lastChange, min, max, warn, inact, expire, flag
  193. )
  194. )
  195. def getspnam(self, username: str) -> _ShadowRecord:
  196. """
  197. Return the shadow user record corresponding to the given username.
  198. """
  199. if not isinstance(username, str):
  200. raise TypeError(f"getspnam() argument must be str, not {type(username)}")
  201. for entry in self._users:
  202. if entry.sp_nam == username:
  203. return entry
  204. raise KeyError(username)
  205. def getspall(self):
  206. """
  207. Return a list of all shadow user records.
  208. """
  209. return self._users