Пример #1
0
    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]}
Пример #2
0
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