Ejemplo n.º 1
0
def register_new_team(request, session, user):
    """Create a new team.

    Args:
        request: (Request)
        session: (DBSession)
        user: (User) user creating the team

    Returns:
        (None|tid): None if something failed, tid otherwise
    """
    tid = request.params.get('team_id', "")
    if len(tid) == 0:
        request.session.flash("Enter a team id first", 'warning')
        return None

    tid = tid.lower().strip()
    if " " in tid:
        msg = "Team id ('%s') cannot have space" % tid
        request.session.flash(msg, 'warning')
        return None

    team = Team.get(session, tid)
    if team is not None:
        team_url = request.route_url('team_view_home', uid=tid)
        msg = "Team <a href='%s'>'%s'</a> already exists" % (team_url,
                                                             tid)
        request.session.flash(Markup(msg), 'warning')
        return None

    # create new team
    team = Team.create(session, tid)
    team.add_policy(session, user, Role.edit)
    request.session.flash("New team %s created" % tid, 'success')
    return tid
Ejemplo n.º 2
0
def view_init(request, session, tab):
    """Common init for all 'view' parts.

    Args:
        request: (Request)
        session: (DBSession)
        tab: (str) current tab in view

    Returns:
        (Team, dict of (str: any)): team, view_params
    """
    tid = request.matchdict['uid']
    team = Team.get(session, tid)
    if team is None:
        request.session.flash("Team %s does not exists" % tid, 'warning')
        raise HTTPFound(location=request.route_url('home'))

    current_uid = request.unauthenticated_userid

    # allow edition
    allow_edit = (current_uid is not None and
                  team.access_role(session, current_uid) == Role.edit)

    view_params = {"team": team,
                   "tabs": tabs,
                   "tab": tab,
                   "allow_edit": allow_edit,
                   "sections": []}

    return team, view_params
Ejemplo n.º 3
0
def view(request):
    if request.unauthenticated_userid is not None:
        request.session.flash("Already logged in, log out first", 'warning')
        return HTTPFound(location=request.route_url('home'))

    if "ok" in request.params:
        session = DBSession()
        view_params = {}
        for field_name in ("user_id", "user_name", "user_email"):
            if field_name in request.params:
                view_params[field_name] = request.params[field_name]

        # check all fields are correct
        uid = request.params["user_id"]
        if len(uid) == 0 or not is_good_id(uid):
            request.session.flash("User id is not a valid id", 'warning')
            return view_params

        name = request.params["user_name"]
        if len(name) == 0 or not is_good_name(name):
            request.session.flash("Name given is not valid", 'warning')
            return view_params

        email = request.params["user_email"]
        if len(email) == 0 or not is_good_email(email):
            request.session.flash("Email given is not valid", 'warning')
            return view_params

        # check user does not exist already
        # as a user
        user = User.get(session, uid)
        if user is not None:
            request.session.flash("User %s already exists" % uid, 'warning')
            return view_params

        # as a team
        team = Team.get(session, uid)
        if team is not None:
            msg = "User %s already exists as a team name" % uid
            request.session.flash(msg, 'warning')
            return view_params

        # register new user
        User.create(session, uid, name, email)
        return log_user_in(request, uid, True)

    else:
        return {}
Ejemplo n.º 4
0
def register_new_user(request, session, team, new_uid):
    """Register a new user according to info in form

    Args:
        request: (Request)
        session: (DBSession)
        team: (Team)
        new_uid: (str) id of user to add to team auth

    Returns:
        (bool): whether team has changed and need to be reloaded
    """
    if new_uid == team.id:
        msg = "Cannot be a member of itself"
        request.session.flash(msg, 'warning')
        return False

    role = Role.from_str(request.params.get("role_new", "denied"))

    member = User.get(session, new_uid)
    if member is not None:
        if new_uid in (pol.actor for pol in team.auth):
            msg = "%s already a direct member" % member.id
            request.session.flash(msg, 'warning')
            return False

        team.add_policy(session, member, role)
        request.session.flash("New member %s added" % member.id, 'success')
        return True

    member = Team.get(session, new_uid)
    if member is not None:
        if team.has_member(session, new_uid):
            request.session.flash("%s already a member" % member.id, 'warning')
            return False

        if member.has_member(session, team.id):
            msg = "Circular reference %s is a member of %s" % (team.id,
                                                               member.id)
            request.session.flash(msg, 'warning')
            return False

        team.add_policy(session, member, role)
        request.session.flash("New member %s added" % member.id, 'success')
        return True

    request.session.flash("User %s does not exists" % new_uid, 'warning')
    return False
Ejemplo n.º 5
0
def view(request):
    session = DBSession()
    user, view_params = view_init(request, session, 'teams')

    if 'new_team' in request.params and user.id == request.unauthenticated_userid:
        tid = register_new_team(request, session, user)
        if tid is not None:
            loc = request.route_url("team_view_home", uid=tid)
            return HTTPFound(location=loc)

    teams = []
    for actor in user.teams:
        team = Team.get(session, actor.team)
        role = team.access_role(session, request.unauthenticated_userid)
        if role != Role.denied:
            teams.append((role, team))

    view_params['teams'] = teams

    return view_params