def populateDefaultUsers(): userService = support.entityFor(IUserService) assert isinstance(userService, IUserService) userRbacService = support.entityFor(IUserRbacService) assert isinstance(userRbacService, IUserRbacService) users = userService.getAll(limit=1, q=QUser(name='Janet')) if not users: user = User() user.FirstName = 'Janet' user.LastName = 'Editor' user.EMail = '*****@*****.**' user.Name = 'admin' user.Password = hashlib.sha512(b'a').hexdigest() user.Id = userService.insert(user) else: user = next(iter(users)) userRbacService.assignRole(user.Id, blogRoleAdministratorId()) for name in (('Andrew', 'Reporter'), ('Christine', 'Journalist')): users = userService.getAll(limit=1, q=QUser(name=name[0])) if not users: user = User() user.FirstName = name[0] user.LastName = name[1] user.EMail = '*****@*****.**' % name user.Name = name[1].lower() user.Password = hashlib.sha512(b'a').hexdigest() user.Id = userService.insert(user) else: user = next(iter(users)) userRbacService.assignRole(user.Id, blogRoleCollaboratorId())
def populateDefaultUsers(): userService = support.entityFor(IUserService) assert isinstance(userService, IUserService) userRbacService = support.entityFor(IUserRbacService) assert isinstance(userRbacService, IUserRbacService) users = userService.getAll(limit=1, q=QUser(name='admin')) if not users: user = User() user.FirstName = 'Janet' user.LastName = 'Editor' user.EMail = '*****@*****.**' user.Name = 'admin' user.Password = hashlib.sha512(b'a').hexdigest() user.Id = userService.insert(user) else: user = next(iter(users)) userRbacService.assignRole(user.Id, blogRoleAdministratorId()) for name in (('Andrew', 'Reporter'), ('Christine', 'Journalist')): loginName = name[1].lower() users = userService.getAll(limit=1, q=QUser(name=loginName)) if not users: user = User() user.FirstName = name[0] user.LastName = name[1] user.EMail = '*****@*****.**' % name user.Name = loginName user.Password = hashlib.sha512(b'a').hexdigest() user.Id = userService.insert(user) else: user = next(iter(users)) userRbacService.assignRole(user.Id, blogRoleCollaboratorId())
def populateRootUser(): userService = support.entityFor(IUserService) assert isinstance(userService, IUserService) userRbacService = support.entityFor(IUserRbacService) assert isinstance(userRbacService, IUserRbacService) users = userService.getAll(limit=1, q=QUser(name='Janet')) if not users: user = User() user.Name = 'root' user.Password = hashlib.sha512(b'root').hexdigest() user.Id = userService.insert(user) else: user = users[0] userRbacService.assignRole(user.Id, rootRoleId())
def populateRootUser(): userService = support.entityFor(IUserService) assert isinstance(userService, IUserService) userRbacService = support.entityFor(IUserRbacService) assert isinstance(userRbacService, IUserRbacService) roleService = support.entityFor(IRoleService) assert isinstance(roleService, IRoleService) users = userService.getAll(limit=1, q=QUser(name='root')) if not users: user = User() user.Name = 'root' user.Password = hashlib.sha512(b'root').hexdigest() user.Id = userService.insert(user) else: user = users[0] userRbacService.assignRole(user.Id, roleService.getByName(NAME_ROOT).Id)
def setIcon(self, personId, metaDataId, update): ''' @see: IPersonIconService.setIcon ''' if update: user = User() user.Id = personId self.userService.update(user) entityDb = PersonIconMapped() entityDb.Id, entityDb.MetaData = personId, metaDataId try: self.session().merge(entityDb) self.session().flush((entityDb,)) except SQLAlchemyError as e: handle(e, entityDb) return entityDb.Id
def _getCollaboratorForAuthor(self, author, creator, source): ''' Returns a collaborator identifier for the user/source defined in the post. If the post was not created by a user (it is twitter, facebook, etc. post) it returns a collaborator for the user that has added the post. @param author: dict The author data in JSON decoded format @param creator: dict The creator data in JSON decoded format @param source: Source The source from which the blog synchronization is done @return: integer The collaborator identifier. ''' assert isinstance(source, Source) user = User() isAuthor = False if 'User' in author: userJSON = author['User'] isAuthor = True else: userJSON = creator #To support old instances that don't have Uuid attribute if 'Uuid' in userJSON: user.Uuid = userJSON.get('Uuid', '') else: user.Uuid = str(uuid4().hex) if 'Cid' in userJSON: cid = int(userJSON.get('Cid', '')) else: cid = None user.Name = user.Uuid user.FirstName, user.LastName = userJSON.get('FirstName', ''), userJSON.get( 'LastName', '') user.Address, user.PhoneNumber = userJSON.get('Address', ''), userJSON.get( 'PhoneNumber', '') user.EMail, user.Password = userJSON.get('EMail', ''), '~' user.Type = self.user_type_key needUpdate = True try: userId = self.userService.insert(user) except InputError: localUser = self.userService.getByUuid(user.Uuid) userId = localUser.Id if localUser.Type == self.user_type_key and (cid is None or localUser.Cid < cid): user.Id = localUser.Id user.Type = localUser.Type user.Cid = cid self.userService.update(user) else: needUpdate = False collaborator = Collaborator() collaborator.User, collaborator.Source = userId, source.Id try: collaboratorId = self.collaboratorService.insert(collaborator) except InputError: collaborators = self.collaboratorService.getAll(userId, source.Id) collaboratorId = collaborators[0].Id if isAuthor: return [collaboratorId, userId, needUpdate, isAuthor] else: q = QSource(name=author['Source']['Name'], isModifiable=False) sources = self.sourceService.getAll(q=q) if not sources: raise Exception('Invalid source %s' % q.name) collaborators = self.collaboratorService.getAll( userId=None, sourceId=sources[0].Id) if collaborators: return [collaborators[0].Id, userId, needUpdate, isAuthor] else: collaborator = Collaborator() collaborator.Source = sources[0].Id return [ self.collaboratorService.insert(collaborator), userId, needUpdate, isAuthor ]
def _getCollaboratorForAuthor(self, author, creator, source): ''' Returns a collaborator identifier for the user/source defined in the post. If the post was not created by a user (it is twitter, facebook, etc. post) it returns a collaborator for the user that has added the post. @param author: dict The author data in JSON decoded format @param creator: dict The creator data in JSON decoded format @param source: Source The source from which the blog synchronization is done @return: integer The collaborator identifier. ''' assert isinstance(source, Source) user = User() isAuthor = False if 'User' in author: userJSON = author['User'] isAuthor = True else: userJSON = creator #To support old instances that don't have Uuid attribute if 'Uuid' in userJSON: user.Uuid = userJSON.get('Uuid', '') else: user.Uuid = str(uuid4().hex) if 'Cid' in userJSON: cid = int(userJSON.get('Cid', '')) else: cid = None user.Name = user.Uuid user.FirstName, user.LastName = userJSON.get('FirstName', ''), userJSON.get('LastName', '') user.Address, user.PhoneNumber = userJSON.get('Address', ''), userJSON.get('PhoneNumber', '') user.EMail, user.Password = userJSON.get('EMail', ''), '~' user.Type = self.user_type_key needUpdate = False try: userId = self.userService.insert(user) except InputError: localUser = self.userService.getByUuid(user.Uuid) userId = localUser.Id if localUser.Type == self.user_type_key and (cid is None or localUser.Cid < cid): needUpdate = True user.Id = localUser.Id user.Type = localUser.Type user.Cid = cid self.userService.update(user) collaborator = Collaborator() collaborator.User, collaborator.Source = userId, source.Id try: collaboratorId = self.collaboratorService.insert(collaborator) except InputError: collaborators = self.collaboratorService.getAll(userId, source.Id) collaboratorId = collaborators[0].Id if isAuthor: return [collaboratorId, userId, needUpdate, isAuthor] else: q = QSource(name=author['Source']['Name'], isModifiable=False) sources = self.sourceService.getAll(q=q) if not sources: raise Exception('Invalid source %s' % q.name) collaborators = self.collaboratorService.getAll(userId=None, sourceId=sources[0].Id) if collaborators: return [collaborators[0].Id, userId, needUpdate, isAuthor] else: collaborator = Collaborator() collaborator.Source = sources[0].Id return [self.collaboratorService.insert(collaborator), userId, needUpdate, isAuthor]