示例#1
0
 def flushchanges(self, *args):
     from bq.core.model import DBSession
     #transaction.commit()
     #for r in args:
     #    if r:
     #        DBSession.merge(r)
     DBSession.flush()
示例#2
0
def invited_user(email):
    # Validate email with https://github.com/JoshData/python-email-validator  ?
    # Setup a temporary user so that we can add the ACL
    # Also setup the pre-registration info they can
    name = email.split('@', 1)[0]
    count = 1
    check_name = name
    while True:
        user = DBSession.query(BQUser).filter_by(
            resource_name=check_name).first()
        if user is None:
            name = check_name
            break
        check_name = name + str(count)
        count += 1

    password = ''.join(random.SystemRandom().choice(string.ascii_uppercase +
                                                    string.digits)
                       for _ in range(6))
    log.debug('AUTH: tg_user name=%s email=%s display=%s', name, email, email)
    tg_user = User(user_name=name,
                   password=password,
                   email_address=email,
                   display_name=email)
    try:
        DBSession.add(tg_user)
        DBSession.flush()
        data_service.cache_invalidate("/data_service/user")
    except sqlalchemy.exc.IntegrityError:
        log.exception('During user invitation')
    log.debug("AUTH: tg_user = %s", str(tg_user))

    # we should have created the BQUser by now.
    user = DBSession.query(BQUser).filter_by(resource_name=name).first()
    return (user, password)
示例#3
0
 def setUp(self):
     """Prepare model test fixture."""
     try:
         new_attrs = {}
         new_attrs.update(self.attrs)
         new_attrs.update(self.do_get_dependencies())
         self.obj = self.klass(**new_attrs)  #pylint: disable=not-callable
         DBSession.add(self.obj)
         DBSession.flush()
         return self.obj
     except Exception:
         DBSession.rollback()
         raise
示例#4
0
 def load(self, token, **kw):
     """ Return a db records for resource, user, and acl
     @param token: A user uniq
     @return A triple (resource, user, acl) or None,None,None if not unable
     """
     log.debug("Load %s", str(token))
     # Can we read the ACLs at all?
     resource = check_access(request.bisque.parent, RESOURCE_READ)
     # token should be a resource_uniq of user .. so join Taggable(user) and  TaggableAcl
     if resource is None:
         return (None, None, None)
     DBSession.flush()
     acl, user = DBSession.query(TaggableAcl, Taggable).filter(
         TaggableAcl.taggable_id == resource.id,
         TaggableAcl.user_id == Taggable.id,
         Taggable.resource_uniq == token).first()
     return resource, user, acl
示例#5
0
    def new_user (cls, email, password, create_tg = False):
        bquser =  cls( user_name= email,
                       email_address=email,
                       display_name=email,
                       password = password)
        DBSession.add (bquser)
        DBSession.flush()
        DBSession.refresh(bquser)
        bquser.owner_id = bquser.id

        if create_tg:
            tg_user = User()
            tg_user.user_name = email
            tg_user.email_address = email
            tg_user.password = password
            tg_user.display_name = email
            #tg_user.dough_user_id = self.id
            DBSession.add(tg_user)
            DBSession.flush()

        return bquser
示例#6
0
 def resource_output(self,
                     resource,
                     response=None,
                     view=None,
                     format=None,
                     progressive=False,
                     **kw):
     #if response is None:
     log.debug("resource_outtput %s", self.uri)
     DBSession.flush()
     if isinstance(resource, list):
         response = etree.Element('resource')
         db2tree(resource, view=view, parent=response, baseuri=self.uri)
     elif resource is not None:
         response = db2tree(resource,
                            view=view,
                            parent=response,
                            baseuri=self.uri,
                            **kw)
     #transaction.commit()
     accept_header = tg.request.headers.get('accept')
     formatter, content_type = find_formatter(format, accept_header)
     tg.response.headers['Content-Type'] = content_type
     return formatter(response, view=view)
示例#7
0
def resource_acl(resource,
                 newauth,
                 user=None,
                 acl=None,
                 notify=False,
                 invalidate=True,
                 action='append'):
    """Create or modify resource acls

    @param resource:  resource (Taggable)
    @param newauth :  an etree of the acl record or None if deleting
    @param user    :  the user (Taggable) of the acl  or None (will be determined from newauth)
    @param acl     :  the acl (TaggableAcl) or None (will be found or created)
    @param notify  :  send an email on state change (boolean)
    @param invalidate: Invalidate caches (boolean)
    @parama delete : Append/modify or Delete record (boolean)

    @return an etree acl record
    """
    log.debug("ACL SET %s", resource)
    user, passwd = match_user(user=user,
                              user_uniq=newauth.get('user'),
                              email=newauth.get('email'))
    if acl is None:
        try:
            DBSession.flush()  # autoflush is off so flush before query
        except sqlalchemy.exc.IntegrityError:
            log.exception("while preparing for query")

        acl = DBSession.query(TaggableAcl).filter_by(taggable_id=resource.id,
                                                     user_id=user.id).first()
        if acl is None:  # Check if newauth is not None or delete is true???
            acl = TaggableAcl()
            acl.taggable_id = resource.id
            acl.user_id = user.id
            acl.action = "read"
            DBSession.add(acl)

    if action == 'delete':
        log.debug("Removing %s from %s  for %s",
                  newauth.get('action', RESOURCE_READ), resource.resource_uniq,
                  user.resource_uniq)
        # http://stackoverflow.com/questions/8306506/deleting-an-object-from-an-sqlalchemy-session-before-its-been-persisted

        if acl in DBSession.new:
            DBSession.expunge(acl)
        else:
            DBSession.delete(acl)
    else:
        log.debug("Changing share on %s for %s action=%s",
                  resource.resource_uniq, user.resource_uniq,
                  newauth.get('action', RESOURCE_READ))
        acl.action = newauth.get('action', RESOURCE_READ)
    #
    # Special actions on sharing  specific resource types
    #
    handler = SHARE_HANDLERS.get(resource.resource_type)
    if handler:
        log.debug("Special share handling with %s", handler)
        handler(resource.resource_uniq, user.resource_uniq, newauth, action)

    # Notify changes if needed
    if notify:
        try:
            notify_user(action, resource, user, passwd)
        except Exception:
            log.exception("While notifying share")

    if invalidate:
        Resource.hier_cache.invalidate_resource(None, user=user.id)

    # Return the new/updated auth element
    return _aclelem(resource, user, acl)