# -*- test-case-name: twisted.python.test.test_fakepwd -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API. """ from __future__ import absolute_import, division __all__ = ['UserDatabase', 'ShadowDatabase'] class _UserRecord(object): """ L{_UserRecord} holds the user data for a single user in L{UserDatabase}. It corresponds to L{pwd.struct_passwd}. See that class for attribute documentation. """ def __init__(self, name, password, uid, gid, gecos, home, shell): self.pw_name = name self.pw_passwd = password self.pw_uid = uid self.pw_gid = gid self.pw_gecos = gecos self.pw_dir = home self.pw_shell = shell def __len__(self): return 7 def __getitem__(self, index): return ( self.pw_name, self.pw_passwd, self.pw_uid, self.pw_gid, self.pw_gecos, self.pw_dir, self.pw_shell)[index] class UserDatabase(object): """ L{UserDatabase} holds a traditional POSIX user data in memory and makes it available via the same API as L{pwd}. @ivar _users: A C{list} of L{_UserRecord} instances holding all user data added to this database. """ def __init__(self): self._users = [] def addUser(self, username, password, uid, gid, gecos, home, shell): """ Add a new user record to this database. @param username: The value for the C{pw_name} field of the user record to add. @type username: C{str} @param password: The value for the C{pw_passwd} field of the user record to add. @type password: C{str} @param uid: The value for the C{pw_uid} field of the user record to add. @type uid: C{int} @param gid: The value for the C{pw_gid} field of the user record to add. @type gid: C{int} @param gecos: The value for the C{pw_gecos} field of the user record to add. @type gecos: C{str} @param home: The value for the C{pw_dir} field of the user record to add. @type home: C{str} @param shell: The value for the C{pw_shell} field of the user record to add. @type shell: C{str} """ self._users.append(_UserRecord( username, password, uid, gid, gecos, home, shell)) def getpwuid(self, uid): """ Return the user record corresponding to the given uid. """ for entry in self._users: if entry.pw_uid == uid: return entry raise KeyError() def getpwnam(self, name): """ Return the user record corresponding to the given username. """ for entry in self._users: if entry.pw_name == name: return entry raise KeyError() def getpwall(self): """ Return a list of all user records. """ return self._users class _ShadowRecord(object): """ L{_ShadowRecord} holds the shadow user data for a single user in L{ShadowDatabase}. It corresponds to C{spwd.struct_spwd}. See that class for attribute documentation. """ def __init__(self, username, password, lastChange, min, max, warn, inact, expire, flag): self.sp_nam = username self.sp_pwd = password self.sp_lstchg = lastChange self.sp_min = min self.sp_max = max self.sp_warn = warn self.sp_inact = inact self.sp_expire = expire self.sp_flag = flag def __len__(self): return 9 def __getitem__(self, index): return ( self.sp_nam, self.sp_pwd, self.sp_lstchg, self.sp_min, self.sp_max, self.sp_warn, self.sp_inact, self.sp_expire, self.sp_flag)[index] class ShadowDatabase(object): """ L{ShadowDatabase} holds a shadow user database in memory and makes it available via the same API as C{spwd}. @ivar _users: A C{list} of L{_ShadowRecord} instances holding all user data added to this database. @since: 12.0 """ def __init__(self): self._users = [] def addUser(self, username, password, lastChange, min, max, warn, inact, expire, flag): """ Add a new user record to this database. @param username: The value for the C{sp_nam} field of the user record to add. @type username: C{str} @param password: The value for the C{sp_pwd} field of the user record to add. @type password: C{str} @param lastChange: The value for the C{sp_lstchg} field of the user record to add. @type lastChange: C{int} @param min: The value for the C{sp_min} field of the user record to add. @type min: C{int} @param max: The value for the C{sp_max} field of the user record to add. @type max: C{int} @param warn: The value for the C{sp_warn} field of the user record to add. @type warn: C{int} @param inact: The value for the C{sp_inact} field of the user record to add. @type inact: C{int} @param expire: The value for the C{sp_expire} field of the user record to add. @type expire: C{int} @param flag: The value for the C{sp_flag} field of the user record to add. @type flag: C{int} """ self._users.append(_ShadowRecord( username, password, lastChange, min, max, warn, inact, expire, flag)) def getspnam(self, username): """ Return the shadow user record corresponding to the given username. """ for entry in self._users: if entry.sp_nam == username: return entry raise KeyError def getspall(self): """ Return a list of all shadow user records. """ return self._users
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
_pydoctortemplates | Folder | 0755 |
|
|
test | Folder | 0755 |
|
|
__init__.py | File | 674 B | 0644 |
|
_appdirs.py | File | 788 B | 0644 |
|
_inotify.py | File | 3.37 KB | 0644 |
|
_oldstyle.py | File | 2.53 KB | 0644 |
|
_release.py | File | 18.03 KB | 0644 |
|
_sendmsg.c | File | 15.42 KB | 0644 |
|
_setup.py | File | 12.6 KB | 0644 |
|
_shellcomp.py | File | 23.76 KB | 0644 |
|
_textattributes.py | File | 8.87 KB | 0644 |
|
_tzhelper.py | File | 3.12 KB | 0644 |
|
_url.py | File | 253 B | 0644 |
|
compat.py | File | 21.91 KB | 0644 |
|
components.py | File | 13.96 KB | 0644 |
|
constants.py | File | 544 B | 0644 |
|
context.py | File | 3.93 KB | 0644 |
|
deprecate.py | File | 26.15 KB | 0644 |
|
failure.py | File | 23.38 KB | 0644 |
|
fakepwd.py | File | 5.99 KB | 0644 |
|
filepath.py | File | 57.51 KB | 0644 |
|
formmethod.py | File | 11.19 KB | 0644 |
|
htmlizer.py | File | 3.27 KB | 0644 |
|
lockfile.py | File | 7.54 KB | 0644 |
|
log.py | File | 21.95 KB | 0644 |
|
logfile.py | File | 9.85 KB | 0644 |
|
modules.py | File | 26.5 KB | 0644 |
|
monkey.py | File | 2.17 KB | 0644 |
|
procutils.py | File | 1.39 KB | 0644 |
|
randbytes.py | File | 3.87 KB | 0644 |
|
rebuild.py | File | 9.04 KB | 0644 |
|
reflect.py | File | 19.02 KB | 0644 |
|
release.py | File | 1.16 KB | 0644 |
|
roots.py | File | 7.23 KB | 0644 |
|
runtime.py | File | 6.13 KB | 0644 |
|
sendmsg.py | File | 3.34 KB | 0644 |
|
shortcut.py | File | 2.2 KB | 0644 |
|
syslog.py | File | 3.64 KB | 0644 |
|
systemd.py | File | 2.77 KB | 0644 |
|
text.py | File | 5.35 KB | 0644 |
|
threadable.py | File | 3.22 KB | 0644 |
|
threadpool.py | File | 9.61 KB | 0644 |
|
twisted-completion.zsh | File | 1.33 KB | 0644 |
|
url.py | File | 244 B | 0644 |
|
urlpath.py | File | 8.87 KB | 0644 |
|
usage.py | File | 34.19 KB | 0644 |
|
util.py | File | 27.29 KB | 0644 |
|
versions.py | File | 322 B | 0644 |
|
win32.py | File | 5.42 KB | 0644 |
|
zippath.py | File | 9.02 KB | 0644 |
|
zipstream.py | File | 9.53 KB | 0644 |
|