1 # -*- test-case-name: twisted.python.test.test_fakepwd -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API.
10 __all__ = ['UserDatabase', 'ShadowDatabase']
13 class _UserRecord(object):
15 L{_UserRecord} holds the user data for a single user in L{UserDatabase}.
16 It corresponds to L{pwd.struct_passwd}. See that class for attribute
19 def __init__(self, name, password, uid, gid, gecos, home, shell):
21 self.pw_passwd = password
33 def __getitem__(self, index):
35 self.pw_name, self.pw_passwd, self.pw_uid,
36 self.pw_gid, self.pw_gecos, self.pw_dir, self.pw_shell)[index]
40 class UserDatabase(object):
42 L{UserDatabase} holds a traditional POSIX user data in memory and makes it
43 available via the same API as L{pwd}.
45 @ivar _users: A C{list} of L{_UserRecord} instances holding all user data
46 added to this database.
52 def addUser(self, username, password, uid, gid, gecos, home, shell):
54 Add a new user record to this database.
56 @param username: The value for the C{pw_name} field of the user
58 @type username: C{str}
60 @param password: The value for the C{pw_passwd} field of the user
62 @type password: C{str}
64 @param uid: The value for the C{pw_uid} field of the user record to
68 @param gid: The value for the C{pw_gid} field of the user record to
72 @param gecos: The value for the C{pw_gecos} field of the user record
76 @param home: The value for the C{pw_dir} field of the user record to
80 @param shell: The value for the C{pw_shell} field of the user record to
84 self._users.append(_UserRecord(
85 username, password, uid, gid, gecos, home, shell))
88 def getpwuid(self, uid):
90 Return the user record corresponding to the given uid.
92 for entry in self._users:
93 if entry.pw_uid == uid:
98 def getpwnam(self, name):
100 Return the user record corresponding to the given username.
102 for entry in self._users:
103 if entry.pw_name == name:
110 Return a list of all user records.
116 class _ShadowRecord(object):
118 L{_ShadowRecord} holds the shadow user data for a single user in
119 L{ShadowDatabase}. It corresponds to C{spwd.struct_spwd}. See that class
120 for attribute documentation.
122 def __init__(self, username, password, lastChange, min, max, warn, inact,
124 self.sp_nam = username
125 self.sp_pwd = password
126 self.sp_lstchg = lastChange
130 self.sp_inact = inact
131 self.sp_expire = expire
139 def __getitem__(self, index):
141 self.sp_nam, self.sp_pwd, self.sp_lstchg, self.sp_min,
142 self.sp_max, self.sp_warn, self.sp_inact, self.sp_expire,
147 class ShadowDatabase(object):
149 L{ShadowDatabase} holds a shadow user database in memory and makes it
150 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.
161 def addUser(self, username, password, lastChange, min, max, warn, inact,
164 Add a new user record to this database.
166 @param username: The value for the C{sp_nam} field of the user record to
168 @type username: C{str}
170 @param password: The value for the C{sp_pwd} field of the user record to
172 @type password: C{str}
174 @param lastChange: The value for the C{sp_lstchg} field of the user
176 @type lastChange: C{int}
178 @param min: The value for the C{sp_min} field of the user record to add.
181 @param max: The value for the C{sp_max} field of the user record to add.
184 @param warn: The value for the C{sp_warn} field of the user record to
188 @param inact: The value for the C{sp_inact} field of the user record to
192 @param expire: The value for the C{sp_expire} field of the user record
196 @param flag: The value for the C{sp_flag} field of the user record to
200 self._users.append(_ShadowRecord(
201 username, password, lastChange,
202 min, max, warn, inact, expire, flag))
205 def getspnam(self, username):
207 Return the shadow user record corresponding to the given username.
209 for entry in self._users:
210 if entry.sp_nam == username:
217 Return a list of all shadow user records.