async def updateUserName(self, id, userName): r = await self.collection.update_one( {UserManager.UserID: id}, {'$set': { UserManager.Name: userName }}) if r.modified_count == 0: raise IFException('Failed to update UserName.')
async def updateAvatar(self, id, avatar): r = await self.collection.update_one( {UserManager.UserID: id}, {'$set': { UserManager.Avatar: avatar }}) if r.modified_count == 0: raise IFException('Failed to update Avatar.')
async def createUser(self, id, name=None, password=None, privilege=[], avatar=None): if await self.hasUser(id): raise IFException('User [{}] exists.'.format(id)) if name == None: name = id.split('@')[0] if password == None: password = self.__randomPassword() saltA = self.__randomSalt() saltB = self.__randomSalt() hashedPassword = self.__hashPassword(password, [saltA, saltB]) privilege = list(set(privilege)) await self.collection.insert_one({ UserManager.UserID: id, UserManager.Name: name, UserManager.SaltA: saltA, UserManager.SaltB: saltB, UserManager.HashedPassword: hashedPassword, UserManager.Privilege: privilege, UserManager.Avatar: avatar, })
async def updateDescription(self, launcherName, description): r = await self.collection.update_one( {IFLocal.LauncherName: launcherName}, {'$set': { IFLocal.Description: description }}) if r.modified_count == 0: raise IFException('Failed to update Description.')
async def updateBrief(self, launcherName, brief): r = await self.collection.update_one( {IFLocal.LauncherName: launcherName}, {'$set': { IFLocal.Brief: brief }}) if r.modified_count == 0: raise IFException('Failed to update Brief.')
async def removePrivilege(self, id, onePrivilege): P = await self.getPrivilege(id) if P.__contains__(onePrivilege): P.remove(onePrivilege) await self.updatePrivilege(id, P) else: raise IFException('User [{}] does not has provilege [{}].'.format( id, onePrivilege))
async def getCommands(self, launcherName): commands = await self.collection.find_one( {IFLocal.LauncherName: launcherName}, { IFLocal.Commands: 1, '_id': 0 }) if commands == None: raise IFException('Launcher [{}] not exist.'.format(launcherName)) return commands[IFLocal.Commands]
async def getDescription(self, launcherName): description = await self.collection.find_one( {IFLocal.LauncherName: launcherName}, { IFLocal.Description: 1, '_id': 0 }) if description == None: raise IFException('Launcher [{}] not exist.'.format(launcherName)) return description[IFLocal.Description]
async def getBrief(self, launcherName): brief = await self.collection.find_one( {IFLocal.LauncherName: launcherName}, { IFLocal.Brief: 1, '_id': 0 }) if brief == None: raise IFException('Launcher [{}] not exist.'.format(launcherName)) return brief[IFLocal.Brief]
async def createLauncher(self, launcherName, brief=None, description=None): if await self.hasLauncher(launcherName): raise IFException('Launcher [{}] exists.'.format(launcherName)) await self.collection.insert_one({ IFLocal.LauncherName: launcherName, IFLocal.Brief: brief, IFLocal.Description: description, IFLocal.Commands: {} })
async def updatePrivilege(self, id, privilege): privilege = list(set(privilege)) r = await self.collection.update_one( {UserManager.UserID: id}, {'$set': { UserManager.Privilege: privilege }}) if r.modified_count == 0: raise IFException('Failed to clear Privilege.')
async def getBriefAndDescription(self, launcherName): bad = await self.collection.find_one( {IFLocal.LauncherName: launcherName}, { IFLocal.Brief: 1, IFLocal.Description: 1, '_id': 0 }) if bad == None: raise IFException('Launcher [{}] not exist.'.format(launcherName)) return [bad[IFLocal.Brief], bad[IFLocal.Description]]
async def getUserInfo(self, id): info = await self.collection.find_one({UserManager.UserID: id}, { UserManager.Name: 1, UserManager.Avatar: 1, UserManager.Privilege: 1, '_id': 0 }) if info == None: raise IFException('User [{}] not exist.'.format(id)) return info
async def __getHashedPassword(self, id): info = await self.collection.find_one({UserManager.UserID: id}, { UserManager.SaltA: 1, UserManager.SaltB: 1, UserManager.HashedPassword: 1, '_id': 0 }) if info == None: raise IFException('User [{}] not exist.'.format(id)) return info
async def updatePassword(self, id, pwd): saltA = self.__randomSalt() saltB = self.__randomSalt() hashedPassword = self.__hashPassword(pwd, [saltA, saltB]) r = await self.collection.update_one({UserManager.UserID: id}, { '$set': { UserManager.SaltA: saltA, UserManager.SaltB: saltB, UserManager.HashedPassword: hashedPassword, } }) if r.modified_count == 0: raise IFException('Failed to update Password.')