Esempio n. 1
0
def insert_role(user=None):
    """
        Insert un role

        .. :quickref: User;

        @TODO : Ne devrait pas être là mais dans UserHub
        Utilisé dans l'authentification du CAS INPN
    """
    if user:
        data = user
    else:
        data = dict(request.get_json())
    user = User(**data)
    if user.id_role is not None:
        exist_user = DB.session.query(User).get(user.id_role)
        if exist_user:
            DB.session.merge(user)
        else:
            DB.session.add(user)
    else:
        DB.session.add(user)
    DB.session.commit()
    DB.session.flush()
    return user.as_dict()
Esempio n. 2
0
 def create_user(username, organisme=None, scope=None):
     # do not commit directly on current transaction, as we want to rollback all changes at the end of tests
     with db.session.begin_nested():
         user = User(groupe=False,
                     active=True,
                     organisme=organisme,
                     identifiant=username,
                     password=username)
         db.session.add(user)
     # user must have been commited for user.id_role to be defined
     with db.session.begin_nested():
         # login right
         right = UserApplicationRight(id_role=user.id_role,
                                      id_application=app.id_application,
                                      id_profil=profil.id_profil)
         db.session.add(right)
         if scope:
             for action in actions.values():
                 for module in modules:
                     permission = CorRoleActionFilterModuleObject(
                         role=user,
                         action=action,
                         filter=scope,
                         module=module)
                     db.session.add(permission)
     return user
 def user(self, app):
     id_app = app.config['ID_APP']
     with db.session.begin_nested():
         user = User(groupe=False, active=True, identifiant='user', password='******')
         db.session.add(user)
         application = Application.query.get(id_app)
         profil = ProfilsForApp.query.filter_by(id_application=application.id_application) \
                                     .order_by(ProfilsForApp.id_profil.desc()).first().profil
         right = UserApplicationRight(role=user, id_profil=profil.id_profil, id_application=application.id_application)
         db.session.add(right)
     return user
Esempio n. 4
0
def insert_role(user=None):
    """
        Insert un role
        @TODO : Ne devrait pas être là mais dans UserHub
        Utilisé dans l'authentification du CAS INPN
    """
    if user:
        data = user
    else:
        data = dict(request.get_json())
    user = User(**data)
    if user.id_role is not None:
        exist_user = DB.session.query(User).get(user.id_role)
        if exist_user:
            DB.session.merge(user)
        else:
            DB.session.add(user)
    else:
        DB.session.add(user)
    DB.session.commit()
    DB.session.flush()
    return user.as_dict()
Esempio n. 5
0
def route_user(id_user):
    '''
        Route pour les informations de l'utilisateur
        ou pour le login ( si id_user vide ou 0)
    '''

    user = None
    modify = False

    current_user = session.get('current_user', None)

    # current user
    if not id_user and current_user:

        id_user = current_user['id_role']
        user = get_user(id_user)
        modify = True

    # register
    elif not id_user:

        user = User().as_dict()

    # user plus pour les responsable?
    elif id_user and current_user:

        user = get_user(id_user)

        cond_org = (
            current_user['id_droit_max'] >= 3 and
            current_user['id_organisme'] == user['id_organisme']
        )

        if not current_user['id_droit_max'] >= 4 and not cond_org:

            user = None

        modify = current_user['id_role'] == user['id_role'] or current_user['id_droit_max'] >= 5

    return render_template(
        'modules/oeasc/user/user.html',
        user=user,
        current_user=user,
        modify=modify,
        nomenclature=nomenclature_oeasc(),
        config=config,
        liste_organismes_oeasc=get_liste_organismes_oeasc())
Esempio n. 6
0
def get_user(id_declarant=None):
    '''
        Retourne l'utilisateur ayant pour id_role id_declarant
    '''

    if not id_declarant:
        # return as_dict(User())
        return User().as_dict()

    data = DB.session.query(VUsers).filter(
        VUsers.id_role == id_declarant).first()

    if not data:
        return None

    user_dict = data.as_dict()

    return user_dict
Esempio n. 7
0
def getOneExportThread(id_export, export_format, info_role):
    """
        Run export with thread
    """
    # test if export exists
    if (id_export < 1
            or export_format not in blueprint.config.get('export_format_map')):
        return to_json_resp(
            {
                'api_error': 'invalid_export',
                'message': 'Invalid export or export not found'
            },
            status=404)

    current_app.config.update(
        export_format_map=blueprint.config['export_format_map'])

    filters = {f: request.args.get(f) for f in request.args}
    data = dict(request.get_json())

    # alternative email in payload
    tmp_user = User()
    if 'email' in data:
        tmp_user.email = data['email']

    try:

        @copy_current_request_context
        def get_data(id_export, export_format, info_role, filters, user):
            thread_export_data(id_export, export_format, info_role, filters,
                               user)

        # Test if export is allowed
        try:
            repo.get_export_is_allowed(id_export, info_role)
        except Exception:
            return to_json_resp({'message': "Not Allowed"}, status=403)

        # Test if user have an email
        try:
            user = (DB.session.query(User).filter(
                User.id_role == info_role.id_role).one())
            if not user.email and not tmp_user.email:
                return to_json_resp(
                    {
                        'api_error': 'no_email',
                        'message': "User doesn't have email"
                    },
                    status=500)
        except NoResultFound:
            return to_json_resp(
                {
                    'api_error': 'no_user',
                    'message': "User doesn't exist"
                },
                status=404)

        # Run export
        a = threading.Thread(name="export_data",
                             target=get_data,
                             kwargs={
                                 "id_export": id_export,
                                 "export_format": export_format,
                                 "info_role": info_role,
                                 "filters": filters,
                                 "user": tmp_user if (tmp_user.email) else user
                             })
        a.start()

        return to_json_resp(
            {
                'api_success':
                'in_progress',
                'message':
                'The Process is in progress ! You will receive an email shortly'
            },  # noqua
            status=200)

    except Exception as e:
        LOGGER.critical('%s', e)
        if current_app.config['DEBUG']:
            raise
        return to_json_resp({'api_error': 'logged_error'}, status=400)