Exemple #1
0
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]
Exemple #2
0
    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]}