00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2002 00004 # by 00005 # The Board of Trustees of the 00006 # Leland Stanford Junior University. 00007 # All rights reserved. 00008 # 00009 00010 __facility__ = "Online" 00011 __abstract__ = "User maintenance classes" 00012 __author__ = "S. Tuvi <stuvi@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online" 00013 __date__ = ("$Date: 2005/01/25 03:09:30 $").split(' ')[1] 00014 __version__ = "$Revision: 2.2 $" 00015 __credits__ = "SLAC" 00016 00017 import LATTE.copyright_SLAC 00018 00019 class rcUser(object): 00020 """Class that stores security and identification information about users 00021 """ 00022 def __init__(self, userId, loginId, userName): 00023 self.__userId = userId 00024 self.__loginId = loginId 00025 self.__userName = userName 00026 self.__roles = [] 00027 self.__permissions = [] 00028 00029 def getId(self): 00030 """Returns the three digit user id 00031 00032 \return User Id 00033 """ 00034 return self.__userId 00035 00036 def getLoginId(self): 00037 """Returns the user's login id 00038 00039 \return Login id 00040 """ 00041 return self.__loginId 00042 00043 def getName(self): 00044 """Returns the user name 00045 00046 \return User name 00047 """ 00048 return self.__userName 00049 00050 def getRoles(self): 00051 """Returns the list of roles that this user belongs in 00052 00053 \return List of roles 00054 """ 00055 return self.__roles 00056 00057 def getPermissions(self): 00058 """Returns the list of permissions that this user has 00059 00060 \return List of permissions 00061 """ 00062 return self.__permissions 00063 00064 def addRole(self, role): 00065 """Adds \a role to this user's roles 00066 00067 \param role Role name 00068 """ 00069 self.__roles.append(role) 00070 00071 def addPermission(self, permission): 00072 """Adds \a permission to this user's permissions 00073 00074 \param permission Permission name 00075 """ 00076 self.__permissions.append(permission) 00077 00078 def isAdministrator(self): 00079 """Returns the user's administrator status 00080 00081 \return 1: If this user is an administrator 00082 0: Otherwise 00083 """ 00084 return ('administrator' in self.__roles) 00085 00086 def __str__(self): 00087 return self.getName() 00088 00089 class rcUsers(object): 00090 """A dictionary like class that maintains a list of user objects 00091 """ 00092 def __init__(self): 00093 self.__users = {} 00094 00095 def addUser(self, user): 00096 """Adds the \a user to the list 00097 00098 \param user An rcUser object instance 00099 """ 00100 self.__users[user.getId()] = user 00101 00102 def deleteUser(self, userId): 00103 """Deletes the user from the list based on the \a userId 00104 00105 \param userId The three digit id of the user 00106 00107 \return True: If the user was successfully deleted 00108 False: If the user does not exist 00109 """ 00110 if userId in self.__users: 00111 self.__users.__delitem__(userId) 00112 return True 00113 else: 00114 return False 00115 00116 00117 def getUserByLoginId(self, loginId): 00118 """Retrieves the user based on the \a loginId 00119 00120 \param loginId Login id of the user 00121 00122 \return rcUser object instance 00123 """ 00124 for user in self.__users.values(): 00125 if loginId == user.getLoginId(): 00126 return user 00127 00128 def getUserByName(self, name): 00129 """Retrieves the user based on the \a name 00130 00131 \param name Name of the user 00132 00133 \return rcUser object instance 00134 """ 00135 for user in self.__users.values(): 00136 if name == user.getName(): 00137 return user 00138 00139 def getUser(self, userId): 00140 """Retrieves the user based on \a userid 00141 00142 \param Three digit of the user 00143 00144 \return rcUser object instance 00145 """ 00146 if userId in self.__users: 00147 return self.__users[userId] 00148 00149 def getUsers(self): 00150 """Retrieves all users 00151 00152 \return A dictionary of all users keyed by the user id 00153 """ 00154 return self.__users 00155 00156 def getIdNameDict(self): 00157 """Retrieves all users in the form of a dictionary 00158 where values are user names. Provided for backward compatibility 00159 with the old code. 00160 00161 \return A dictionary of user names keyed by the user id 00162 """ 00163 # Provided for backward compatibility to mimick the old list 00164 userList = {} 00165 for (userId, user) in self.__users.items(): 00166 userList[userId] = user.getName() 00167 return userList 00168