Esempio n. 1
0
def getUserGroups(login_id, groups):
    """ get group for users:
    a) the groups defined by his user_group_memberships
    b) the users who have him assigned as a delegation
    c) the groups of the delegation user.
    """
    if login_id not in groups:
        groups.append(login_id)
    session = Session()
    db_user = session.query(domain.User).filter(domain.User.login == login_id).all()
    if len(db_user) == 1:
        user_id = db_user[0].user_id
        query = (
            session.query(domain.GroupMembership)
            .filter(rdb.and_(domain.GroupMembership.user_id == user_id, domain.GroupMembership.active_p == True))
            .options(eagerload("group"), lazyload("user"))
        )
        results = query.all()
        for result in results:
            if result.group.group_principal_id not in groups:
                groups.append(result.group.group_principal_id)
        results = delegation.get_user_delegations(user_id)
        for result in results:
            if result.login not in groups:
                groups = groups + getUserGroups(result.login, groups)
    return groups
Esempio n. 2
0
def get_user_groups(login):
    """Get group for users:
    a) the groups defined by member
    b) the users who have him assigned as a delegation
    c) the groups of the delegation user.
    """
    def get_groups(user_id):
        principal_ids = []
        session = Session()
        query = session.query(domain.GroupMember).filter(
            rdb.and_(domain.GroupMember.user_id == user_id,
                     domain.GroupMember.active_p == True)).options(
                         eagerload("group"), lazyload("user"))
        for member in query:
            principal_ids.append(member.group.principal_name)
        return principal_ids

    groups = set()
    user = get_user_for_login(login)  # user may be None
    if user:
        groups.add(login)
        user_id = user.user_id
        for elem in get_groups(user_id):
            groups.add(elem)
        for user in delegation.get_user_delegations(user):
            for elem in get_groups(user.user_id):
                groups.add(elem)
            groups.add(user.login)
    return groups
Esempio n. 3
0
def get_user_groups(login_id):
    """Get group for users:
    a) the groups defined by his user_group_memberships
    b) the users who have him assigned as a delegation
    c) the groups of the delegation user.
    """
    def get_groups(user_id):
        principal_ids = []
        session = Session()
        query = session.query(domain.GroupMembership).filter(rdb.and_(
                    domain.GroupMembership.user_id == user_id,
                    domain.GroupMembership.active_p == True)
            ).options(eagerload("group"), lazyload("user"))
        for membership in query:
            principal_ids.append(membership.group.principal_name)
        return principal_ids
    groups = set()
    user = get_user(login_id)  # user may be None
    if user:
        groups.add(login_id)
        user_id = user.user_id
        for elem in get_groups(user_id):
            groups.add(elem)
        for user in delegation.get_user_delegations(user):
            for elem in get_groups(user.user_id):
                groups.add(elem)
            groups.add(user.login)
    return groups
Esempio n. 4
0
def getUserGroups(login_id, groups):
    """ get group for users:
    a) the groups defined by his user_group_memberships
    b) the users who have him assigned as a delegation
    c) the groups of the delegation user.
    """
    if login_id not in groups:
        groups.append(login_id)
    session = Session()
    db_user = session.query(
        domain.User).filter(domain.User.login == login_id).all()
    if len(db_user) == 1:
        user_id = db_user[0].user_id
        query = session.query(domain.GroupMembership).filter(
            rdb.and_(domain.GroupMembership.user_id == user_id,
                     domain.GroupMembership.active_p == True)).options(
                         eagerload('group'), lazyload('user'))
        results = query.all()
        for result in results:
            if (result.group.group_principal_id not in groups):
                groups.append(result.group.group_principal_id)
        results = delegation.get_user_delegations(user_id)
        for result in results:
            if (result.login not in groups):
                groups = groups + getUserGroups(result.login, groups)
    return groups
Esempio n. 5
0
def is_current_or_delegated_user(user):
    """Is this user (a delegation of) the currently logged user?
    """
    current_user = get_login_user()
    # Only if there is a user logged in!
    if current_user:
        if current_user.user_id == user.user_id:
            return True
        for d in delegation.get_user_delegations(current_user):
            if d.user_id == user.user_id:
                return True
    return False
Esempio n. 6
0
def is_current_or_delegated_user(user):
    """Is this user (a delegation of) the currently logged user?
    """
    current_user = get_login_user()
    # Only if there is a user logged in!
    if current_user:
        if current_user.user_id == user.user_id:
            return True
        for d in delegation.get_user_delegations(current_user):
            if d.user_id == user.user_id:
                return True
    return False
Esempio n. 7
0
def user_is_context_owner(context):
    """Test if current user is the context owner e.g. to check if someone 
        manipulating the context object is other than the owner of the object.
        
        A delegate is considered to be an owner of the object
    """
    user = model_utils.get_db_user()
    owner_login = utils.get_owner_login_pi(context)
    if user.login == owner_login:
        return True
    delegations = delegation.get_user_delegations(user.user_id)
    users = [delegate.login for delegate in delegations]
    return owner_login in users
Esempio n. 8
0
def user_is_context_owner(context):
    """Test if current user is the context owner e.g. to check if someone 
        manipulating the context object is other than the owner of the object.
        
        A delegate is considered to be an owner of the object
    """
    user = model_utils.get_db_user()
    owner_login = utils.get_owner_login_pi(context)
    if user.login == owner_login:
        return True
    delegations = delegation.get_user_delegations(user.user_id)
    users = [delegate.login for delegate in delegations]
    return owner_login in users
Esempio n. 9
0
 def constructQuery(self, context):
     mp_query = super(MemberOfParliamentDelegationSource, 
             self).constructQuery(context)
     #XXX clerks cannot yet choose MPs freely
     user_id = utils.get_db_user_id()
     if user_id:
         user_ids = [user_id]
         for result in delegation.get_user_delegations(user_id):
             user_ids.append(result.user_id)
         query = mp_query.filter(
             domain.MemberOfParliament.user_id.in_(user_ids))
         if len(query.all()) > 0:
             return query
     return mp_query
Esempio n. 10
0
 def constructQuery(self, context):
     mp_query = super(MemberOfParliamentDelegationSource,
                      self).constructQuery(context)
     #XXX clerks cannot yet choose MPs freely
     user_id = utils.get_db_user_id()
     if user_id:
         user_ids = [user_id]
         for result in delegation.get_user_delegations(user_id):
             user_ids.append(result.user_id)
         query = mp_query.filter(
             domain.MemberOfParliament.user_id.in_(user_ids))
         if len(query.all()) > 0:
             return query
     return mp_query