Beispiel #1
0
    def local_query_delete(self, query):
        #session.query(User).filter(User.id==7).delete()

        fields = query.fields

        cls = self.map_object[query.object]

        # Transform a Filter into a sqlalchemy expression
        _filters = get_sqla_filters(cls, query.filters)
        _fields = xgetattr(cls, query.fields) if query.fields else None

        res = db.query(*_fields) if _fields else db.query(cls)
        if query.filters:
            for _filter in _filters:
                res = res.filter(_filter)

        # Do we need to limit to the user's own results
        try:
            if self.user and cls.restrict_to_self and self.user[
                    'email'] != ADMIN_USER:
                res = res.filter(cls.user_id == self.user['user_id'])
        except AttributeError:
            pass

        res.delete()
Beispiel #2
0
    def local_query_get(self, query):
        #
        # XXX How are we handling subqueries
        #

        fields = query.fields
        # XXX else tap into metadata

        cls = self.map_object[query.object]

        # Transform a Filter into a sqlalchemy expression
        _filters = get_sqla_filters(cls, query.filters)
        _fields = xgetattr(cls, query.fields) if query.fields else None

        res = db.query(*_fields) if _fields else db.query(cls)
        if query.filters:
            for _filter in _filters:
                res = res.filter(_filter)

        # Do we need to limit to the user's own results
        try:
            if self.user and cls.restrict_to_self and self.user[
                    'email'] != ADMIN_USER:
                res = res.filter(cls.user_id == self.user['user_id'])
        except AttributeError:
            pass
        try:
            tuplelist = res.all()
            return tuplelist
        except SQLAlchemyError, e:
            Log.error("SQLAlchemyError trying to rollback db session: %s" % e)
            db.rollback()
            self.local_query_get(query)
            return list()
Beispiel #3
0
    def process_params(params, filters, user):

        # JSON ENCODED FIELDS are constructed into the json_fields variable
        given = set(params.keys())
        accepted = set([c.name for c in User.__table__.columns])
        given_json_fields = given - accepted

        if given_json_fields:
            if 'config' in given_json_fields:
                raise Exception, "Cannot mix full JSON specification & JSON encoded fields"

            r = db.query(User.config).filter(filters)
            if user:
                r = r.filter(User.user_id == user['user_id'])
            r = r.filter(filters)  #User.platform_id == platform_id)
            r = r.one()
            try:
                json_fields = json.loads(r.config)
            except Exception, e:
                json_fields = {}

            # We First look at convenience fields
            for field in given_json_fields:
                json_fields[field] = params[field]
                del params[field]

            params['config'] = json.dumps(json_fields)
Beispiel #4
0
    def local_query_update(self, query):

        # XXX The filters that are accepted in config depend on the gateway
        # Real fields : user_credential
        # Convenience fields : credential, then redispatched to real fields
        # same for get, update, etc.

        # XXX What about filters on such fields
        # filter works with account and platform table only
        if query.object == 'account' or query.object == 'platform':
            if not query.filters.has_eq(
                    'platform_id') and not query.filters.has_eq('platform'):
                raise Exception, "Cannot update JSON fields on multiple platforms"

        cls = self.map_object[query.object]

        # Note: we can request several values

        # FIELDS: exclude them
        _fields = xgetattr(cls, query.fields)

        # FILTERS: Note we cannot filter on json fields
        _filters = cls.process_filters(query.filters)
        _filters = get_sqla_filters(cls, _filters)

        # PARAMS
        #
        # The fields we can update in params are either:
        # - the original fields, including json encoded ones
        # - fields inside the json encoded ones
        # - convenience fields
        # We refer to the model for transforming the params structure into the
        # final one

        # Password update
        #
        # if there is password update in query.params
        # We encrypt the password according to the encryption of adduser.py
        # As a result from the frontend the edited password will be inserted
        # into the local DB as encrypted
        if 'password' in query.params:
            query.params['password'] = self.encrypt_password(
                query.params['password'])
        _params = cls.process_params(query.params, _filters, self.user)
        # only 2.7+ _params = { getattr(cls, k): v for k,v in query.params.items() }
        _params = dict([(getattr(cls, k), v) for k, v in _params.items()])

        #db.query(cls).update(_params, synchronize_session=False)
        q = db.query(cls)
        for _filter in _filters:
            q = q.filter(_filter)
        if self.user and cls.restrict_to_self and self.user[
                'email'] != ADMIN_USER:
            q = q.filter(getattr(cls, 'user_id') == self.user['user_id'])
        q = q.update(_params, synchronize_session=False)
        try:
            db.commit()
        except:
            db.rollback()
        return []
Beispiel #5
0
    def process_filters(filters):
        user_filters = filters.get('user')
        filters.delete('user')
        if user_filters:
            for uf in user_filters:
                assert uf.op.__name__ == 'eq', "Only == is supported for convenience filter 'user'"
                ret = db.query(User.user_id)
                ret = ret.filter(User.email == uf.value)
                ret = ret.one()
                filters.add(Predicate('user_id', '=', ret[0]))

        platform_filters = filters.get('platform')
        filters.delete('platform')
        if platform_filters:
            for pf in platform_filters:
                assert pf.op.__name__ == 'eq', "Only == is supported for convenience filter 'platform'"
                ret = db.query(Platform.platform_id)
                ret = ret.filter(Platform.platform == pf.value)
                ret = ret.one()
                filters.add(Predicate('platform_id', '=', ret[0]))
        return filters
Beispiel #6
0
    def params_ensure_user(cls, params, user):
        # A user can only create its own objects
        if cls.restrict_to_self:
            params['user_id'] = user['user_id']
            return

        if 'user_id' in params: return
        if 'user' in params:
            user_params = params['user']
            del params['user']
            ret = db.query(User.user_id)
            ret = ret.filter(User.email == user_params)
            ret = ret.one()
            params['user_id'] = ret[0]
            return
        raise Exception, 'User should be specified'
Beispiel #7
0
    def process_params(params, filters, user):

        # PARAMS
        # added by Loic, based on the process_filters functions
        user_params = params.get('user')
        if user_params:
            del params['user']
            #print "user_params=",user_params
            ret = db.query(User.user_id)
            ret = ret.filter(User.email == user_params)
            ret = ret.one()
            params['user_id'] = ret[0]

        platform_params = params.get('platform')
        if platform_params:
            del params['platform']
            #print "platform_params=", platform_params
            ret = db.query(Platform.platform_id)
            ret = ret.filter(Platform.platform == platform_params)
            ret = ret.one()
            params['platform_id'] = ret[0]

        # JSON ENCODED FIELDS are constructed into the json_fields variable
        given = set(params.keys())
        accepted = set([c.name for c in Account.__table__.columns])
        given_json_fields = given - accepted

        if given_json_fields:
            if 'config' in given_json_fields:
                raise Exception, "Cannot mix full JSON specification & JSON encoded fields"

            r = db.query(Account.config)
            for filter in filters:
                r = r.filter(filter)
            if user:
                r = r.filter(Account.user_id == user['user_id'])
            #r = r.filter(filters) #Account.platform_id == platform_id)
            r = r.one()
            try:
                json_fields = json.loads(r.config)
            except Exception, e:
                json_fields = {}

            # We First look at convenience fields
            for field in given_json_fields:
                if field == 'credential':
                    # We'll determine the type of credential
                    # XXX NOTE This is SFA specific... it should be hooked by gateways
                    # @loic modified according to the SFA Gateway, to handle delegation
                    # XXX TODO need to be improved...
                    c = Credential(string=params[field])
                    c_type = c.get_gid_object().get_type()
                    if c_type == 'user':
                        new_field = 'delegated_%s_credential' % c_type
                        json_fields[new_field] = params[field]
                    else:
                        cred_name = 'delegated_%s_credentials' % c_type
                        if not cred_name in json_fields:
                            json_fields[cred_name] = {}
                        c_target = c.get_gid_object().get_hrn()
                        json_fields[cred_name][c_target] = params[field]
                else:
                    json_fields[field] = params[field]
                del params[field]

            params['config'] = json.dumps(json_fields)