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 _getCollaboratorForAuthor(self, author, source): ''' Returns a collaborator identifier for the user/source defined in the post. If the post was not created by a user it returns a collaborator for the source identified in the post and the default user. The default user should be the sync entry creator. If the source from the post does not exist locally raises Exception. @param author: dict The author 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) try: userJSON = author['User'] user = User() user.Name = sha1((userJSON.get('Name', '') + source.URI).encode( self.encodingType)).hexdigest() user.FirstName, user.LastName = userJSON.get('FirstName', ''), userJSON.get( 'LastName', '') user.EMail, user.Password = userJSON.get('EMail', ''), '*' user.Type = self.user_type_key try: userId = self.userService.insert(user) except InputError: localUser = self.userService.getAll(q=QUser(name=user.Name)) userId = localUser[0].Id c = Collaborator() c.User, c.Source = userId, source.Id try: return [self.collaboratorService.insert(c), userId] except InputError: collabs = self.collaboratorService.getAll(userId, source.Id) return [collabs[0].Id, userId] except KeyError: q = QSource(name=author['Source']['Name'], isModifiable=False) sources = self.sourceService.getAll(q=q) if not sources: raise Exception('Invalid source %s' % q.name) collabs = self.collaboratorService.getAll(userId=None, sourceId=sources[0].Id) if collabs: return [collabs[0].Id, None] else: c = Collaborator() c.Source = sources[0].Id return [self.collaboratorService.insert(c), None]
def getUsersIds(): userService = entityFor(IUserService) assert isinstance(userService, IUserService) if not _cache_users: users = _cache_users for name in USERS: usrs = userService.getAll(adminId=None, q=QUser(name=name)) if usrs: users[name] = next(iter(usrs)).Id else: usr = User() usr.Name = name usr.Password = hashlib.sha512(b'a').hexdigest() usr.FirstName, usr.LastName, usr.EMail = USERS[name] users[name] = userService.insert(adminId=None, user=usr) return _cache_users
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 getCollaboratorsIds(): collaboratorService = entityFor(ICollaboratorService) assert isinstance(collaboratorService, ICollaboratorService) collaborators = {} for source, id in getSourcesIds().items(): colls = collaboratorService.getAll(qs=QSource(name=source)) if colls: collaborators[source] = colls[0].Id else: coll = Collaborator() coll.User = None coll.Source = getSourcesIds()[source] collaborators[source] = collaboratorService.insert(coll) for user, id in getUsersIds().items(): colls = collaboratorService.getAll(qu=QUser(name=user)) if colls: collaborators[user] = colls[0].Id else: coll = Collaborator() coll.User = id coll.Source = getSourcesIds()['internal'] collaborators[user] = collaboratorService.insert(coll) return collaborators