Example #1
0
def rest_repo_user(request, repo_name, username):
    repo = Repository(repo_name)
    user = UserFactory.instantiate_user(username)

    # Add user
    if request.method == 'POST':
        try:
            # Get the repository and add the user
            repo.add_user(user)
            repo.add_user_read(user)
            repo.add_user_write(user)
            repo.save()
            return HttpResponse("User " + username + " added to " + repo_name)
        except Exception as e:
            return HttpResponseServerError(e)
    # Delete the user
    if request.method == 'DELETE':
        # Remove the user from the repository
        repo.remove_user(user)
        repo.save()
        return HttpResponse(username + " removed from " + repo_name)
    # Get the user permissions
    if request.method == 'GET':
        permissions = {'read' : False, 'write' : False}
        # retrieve the list of read and write users
        user_read_list = repo.user_read_list
        user_write_list = repo.user_write_list
        # check if the user has read and write access
        if user in user_read_list:
            permissions['read'] = True
        if user in user_write_list:
            permissions['write'] = True
            
        # reply with the json permission object
        json_reply = json.dumps(permissions)
        return HttpResponse(json_reply)
    
    if request.method == 'PUT':
        # retrieve the credentials from the json
        permissions = json.loads(request.raw_post_data)

        # Get the old password and new password
        if 'read' in permissions:
            # add the read permission to the repo
            if permissions['read']:
                repo.add_user_read(user)
            else:
                repo.remove_user_read(user)

        if 'write' in permissions:
            # add the write permission to the repo
            if permissions['write']:
                repo.add_user_write(user)
            else:
                repo.remove_user_write(user)
                
        repo.save()
        return HttpResponse(user.username + "'s permissions updated")
Example #2
0
def rest_repo_user(request, repo_name, username):
    repo = Repository(repo_name)
    user = UserFactory.instantiate_user(username)

    # Add user
    if request.method == 'POST':
        try:
            # Get the repository and add the user
            repo.add_user(user)
            repo.add_user_read(user)
            repo.add_user_write(user)
            repo.save()
            return HttpResponse("User " + username + " added to " + repo_name)
        except Exception as e:
            return HttpResponseServerError(e)
    # Delete the user
    if request.method == 'DELETE':
        try:
            # Remove the user from the repository
            repo.remove_user(user)
            repo.save()
            return HttpResponse(username + " removed from " + repo_name)
        except Exception as e:
            return HttpResponseServerError(e)
    # Get the user permissions
    if request.method == 'GET':
        permissions = {'read': False, 'write': False}
        # retrieve the list of read and write users
        user_read_list = repo.user_read_list
        user_write_list = repo.user_write_list
        # check if the user has read and write access
        if user in user_read_list:
            permissions['read'] = True
        if user in user_write_list:
            permissions['write'] = True

        # reply with the json permission object
        json_reply = json.dumps(permissions)
        return HttpResponse(json_reply)

    if request.method == 'PUT':
        # retrieve the credentials from the json
        permissions = json.loads(request.raw_post_data)

        # Get the old password and new password
        if 'read' in permissions:
            # add the read permission to the repo
            if permissions['read']:
                repo.add_user_read(user)
            else:
                repo.remove_user_read(user)

        if 'write' in permissions:
            # add the write permission to the repo
            if permissions['write']:
                repo.add_user_write(user)
            else:
                repo.remove_user_write(user)

        repo.save()
        return HttpResponse(user.username + "'s permissions updated")