def populate_user_roles(drop): """ Populate default set of user roles into the system. :param drop: Drop the existing collection before trying to populate. :type: boolean """ # define your user roles here # note: you MUST have Administrator, Read Only, and a third option # available! user_roles = ['Administrator', 'Analyst', 'Read Only'] if drop: UserRole.drop_collection() if len(UserRole.objects()) < 1: for role in user_roles: ur = UserRole() ur.name = role ur.save() print "User Roles: added %s roles!" % len(user_roles) else: print "User Roles: existing documents detected. skipping!"
def add_new_user_role(name, analyst): """ Add a new user role to the system. :param name: The name of the role. :type name: str :param analyst: The user adding the role. :type analyst: str :returns: True, False """ from crits.core.user_role import UserRole name = name.strip() role = UserRole.objects(name=name).first() if not role: role = UserRole() role.name = name try: role.save(username=analyst) return True except ValidationError: return False else: return False