def add_user_group_to_domains(): """ Function will add a 'default' UserGroup to each domain """ print "running: add_user_group_to_domains()" domains = Domain.query.all() for domain in domains: user_group = UserGroup.query.filter_by(domain_id=domain.id).first() if not user_group: print "domain_id: {}".format(domain.id) db.session.add(UserGroup(name='default', domain_id=domain.id)) db.session.commit()
def create_user_groups(group_names, domain_ids): """ This function creates user groups in database. It takes group names ad domain ids as input. :param list(string) group_names: user group names :param list(int | long) domain_ids: domain ids :rtype: tuple[list] """ assert len(group_names) == len( domain_ids), 'group names and domain ids should be equal' groups = [] for group_name, domain_id in zip(group_names, domain_ids): user_group = UserGroup.get_by_name(group_name) if not user_group: user_group = UserGroup.add_groups([{ 'name': group_name }], domain_id)[0] logger.debug('create_user_groups: Created a user group: %s', user_group.name) groups.append(user_group) return groups, [group.id for group in groups]
def delete(self, **kwargs): """ DELETE /domain/<domain_id>/groups Remove user_groups from a given domain :return A dictionary containing success message :rtype: dict """ requested_domain_id = kwargs.get('domain_id') requested_domain = Domain.query.get(requested_domain_id) if not requested_domain: raise NotFoundError("Domain with domain_id %s doesn't exist" % requested_domain_id) posted_data = request.get_json(silent=True) if not posted_data or 'groups' not in posted_data: raise InvalidUsage("Request body is empty or not provided") if request.user.role.name != 'TALENT_ADMIN' and requested_domain_id != request.user.domain_id: raise UnauthorizedError("User %s doesn't have appropriate permission to remove user_groups " "from domain %s" % (request.user.id, requested_domain_id)) UserGroup.delete_groups(requested_domain_id, posted_data.get('groups')) return '', 200
def post(self, **kwargs): """ POST /groups/<group_id>/users Add users in a given user_group :return A dictionary containing success message :rtype: dict """ requested_group_id = kwargs.get('group_id') requested_group = UserGroup.query.get(requested_group_id) if not requested_group: raise NotFoundError("Group with group_id %s doesn't exist" % requested_group_id) posted_data = request.get_json(silent=True) if not posted_data or 'user_ids' not in posted_data: raise InvalidUsage("Request body is empty or not provided") if request.user.role.name != 'TALENT_ADMIN' and requested_group.domain_id != request.user.domain_id: raise UnauthorizedError("User %s doesn't have appropriate permission to add users to a " "group %s" % (request.user.id, requested_group_id)) UserGroup.add_users_to_group(requested_group, posted_data.get('user_ids')) return '', 201
def get(self, **kwargs): """ GET /domain/<domain_id>/groups Fetch all user_groups of a given domain :return A dictionary containing id and name of all user_groups of a given domain :rtype: dict """ requested_domain_id = kwargs.get('domain_id') requested_domain = Domain.query.get(requested_domain_id) if not requested_domain: raise NotFoundError("Domain with domain_id %s doesn't exist" % requested_domain_id) if request.user.role.name != 'TALENT_ADMIN' and requested_domain_id != request.user.domain_id: raise UnauthorizedError("User %s doesn't have appropriate permission to get all user_groups " "of domain %s" % (request.user.id, requested_domain_id)) all_user_groups_of_domain = UserGroup.all_groups_of_domain(requested_domain_id) return {"user_groups": [{'id': user_group.id, 'name': user_group.name} for user_group in all_user_groups_of_domain]}
def get(self, **kwargs): """ GET /groups/<group_id>/users?include_stats=True Fetch all users in a user_group and also include stats :return A dictionary containing id and lastName of all users of a user_group :rtype: dict """ requested_group_id = kwargs.get('group_id') include_stats_flag = request.args.get('include_stats', False) requested_group = UserGroup.query.get(requested_group_id) if not requested_group: raise NotFoundError("Group with group_id %s doesn't exist" % requested_group_id) if request.user.role.name != 'TALENT_ADMIN' and requested_group.domain_id != request.user.domain_id: raise UnauthorizedError("User %s doesn't have appropriate permission to add users to a " "group %s" % (request.user.id, requested_group_id)) users_data_dict = {user.id: user.to_dict() for user in UserGroup.all_users_of_group(requested_group_id)} return {"users": get_users_stats_from_mixpanel(users_data_dict, False, include_stats_flag).values()}
def post(self, **kwargs): """ POST /domain/<domain_id>/groups Add user_groups to a given domain :return A dictionary containing ids of user_groups added to given domain :rtype: dict """ requested_domain_id = kwargs.get('domain_id') requested_domain = Domain.query.get(requested_domain_id) if not requested_domain: raise NotFoundError("Domain with domain_id %s doesn't exist" % requested_domain_id) posted_data = request.get_json(silent=True) if not posted_data or 'groups' not in posted_data: raise InvalidUsage("Request body is empty or not provided") if request.user.role.name != 'TALENT_ADMIN' and requested_domain_id != request.user.domain_id: raise UnauthorizedError("User %s doesn't have appropriate permission to add user_groups " "to domain %s" % (request.user.id, requested_domain_id)) user_groups = UserGroup.add_groups(posted_data.get('groups'), requested_domain_id) return {'user_groups': [user_group.id for user_group in user_groups]}
def create_user(email, domain_id, first_name, last_name, expiration, phone="", dice_user_id=None, thumbnail_url='', user_group_id=None, locale=None, role_id=None): temp_password = gen_salt(8) hashed_password = gettalent_generate_password_hash(temp_password) user_group = None # Get user's group ID if not user_group_id: user_groups = UserGroup.all_groups_of_domain(domain_id=domain_id) if user_groups: # TODO: this shouldn't be necessary since each domain must belong to a user_group user_group = user_groups[0] else: user_group = UserGroup.query.get(user_group_id) if not user_group: raise InvalidUsage( "Either user_group_id is not provided or no group exists in user's domain" ) if user_group.domain_id != domain_id: raise InvalidUsage("User Group %s belongs to different domain" % user_group.id) user_data_dict = dict(email=email, domain_id=domain_id, first_name=first_name, last_name=last_name, expiration=expiration, dice_user_id=dice_user_id, password=hashed_password, phone=phone, thumbnail_url=thumbnail_url, user_group_id=user_group.id, locale=locale, is_disabled=False, role_id=role_id) user_data_dict = {k: v for k, v in user_data_dict.items() if v} # Make new entry in user table user = User(**user_data_dict) db.session.add(user) db.session.commit() # TODO: Make new widget_page if first user in domain # TODO: Add activity send_new_account_email(email, temp_password, '*****@*****.**') return user