Exemple #1
0
def login(username, password):
    """ Get username and access token from github """
    gitlab_userdata = get_access_token(username, password)
    if gitlab_userdata is None:
        return None
    access_token = gitlab_userdata.get('private_token')

    if access_token:
        # Check if this user exists in DB
        # if not we need to create it
        user = User.fetch_from_gitlab_token(access_token)
        # Save this new user
        if user is None:
            user = User({
                "username": gitlab_userdata['username'],
                "name": gitlab_userdata['name'],
                #                         "gitlab_url": data['html_url'],
                "email": gitlab_userdata['email'],
                "token_gitlab": access_token,
                "id_gitlab": gitlab_userdata['id'],
            })
            if not user.create():
                return None

        update_user_info_from_gitlab(user.username, user.token_gitlab)
        return {"access_token": access_token, "username": user.username}
    return None
Exemple #2
0
def login(code):
    """ Get username and access token from github """
    if code is None:
        return None

    access_token = get_access_token(code)

    if access_token:
        # Check if this user exists in DB
        # if not we need to create it
        user = User.fetch_from_github_token(access_token)
        if user is None:
            # Get data from github
            data = get_user_from_token(access_token)
            if data:
                # Save this new user
                user = User({
                    "username": data['login'],
                    "name": data['name'],
                    "github_url": data['html_url'],
                    "email": data['email'],
                    "token_github": access_token
                })
                if not user.create():
                    return None
                update_user_info_from_github(user.username, user.token_github)
            else:
                return None
        return {"access_token": access_token, "username": user.username}
    return None
Exemple #3
0
 def post(self, sent_user):
     """Create/Edit user"""
     user = User.fetch(self.username)
     if user is None:
         sent_user.username = self.username
         new_user = User(sent_user.as_dict()) 
         if not new_user.create():
             # Handle error
             return {"result": "Error creating user %s with data %s" % (sent_user.username, sent_user)}
         return {"result": "User %s created" % sent_user.username}
     else:
         if not user.update(sent_user):
             # Handle error
             return {"result": "Error editing"}
         return {"result": "User %s edited" % self.username}
Exemple #4
0
    def get(self, username):
        """ launch build  from gitlab webhook"""
        access_token = pecan.request.GET.get('access_token')
        if access_token:
            # Check if this user exists in DB
            user = User.fetch(username)
            if user is None:
                # This user maybe a group
                data = gitlab.get_group(username, access_token)
                if data is not None:
                    url = "%s/groups/%s" % (pecan.conf.gitlab_url,
                                            data['name'])
                    if pecan.conf.gitlab_secure:
                        url = "https://"
                    else:
                        url = "http://"
                    # Save this new group
                    user = User({
                        "username": data['name'],
                        "name": data['name'],
                        "gitlab_url": url,
                        "gitlab_group": True,
                        "id_gitlab": data['id'],
                    })
                if not user.create():
                    return None
                else:
                    # if user doesn't exists and this is not a group
                    # This request is strange so 403
                    abort(403)
            else:
                # if not we need to create it
                gitlab_user = gitlab.get_user(user.id_gitlab, access_token)
                # Update user info
                # TODO Disabled for now


#                if gitlab_user:
#                    user.name = gitlab_user.get('name')
#                    user.email = gitlab_user.get('email')
#                    if not user.update():
#                        return None
            if user.gitlab_group:
                return gitlab.update_group_info_from_gitlab(user, access_token)
            else:
                return gitlab.update_user_info_from_gitlab(
                    username, access_token)
        return None
Exemple #5
0
 def get(self, username):
     """ launch build  from github webhook"""
     access_token = pecan.request.GET.get('access_token')
     if access_token:
         # Check if this user exists in DB
         # if not we need to create it
         data = github.get_user(username, access_token)
         if data:
             # Save this new user
             user = User({"username": data['login'],
                          "name": data['name'],
                          "github_url": data['html_url'],
                          "email": data['email'],
                          "token_github": access_token
                          })
             if not user.create():
                 return None
         return github.update_user_info_from_github(username, access_token)
     return None