def rest_repository(request): # Add new repository if request.method == 'POST': name = request.POST['name'] try: # check the repo name matcher = re.compile("^\w{1,}$") if matcher.match(name) is None: raise Exception( "Please enter an alphanumeric name without spaces") if (name == ""): raise Exception("Please enter a non empty name") # create the repo repository = Repository(name) repository.create() except WindowsError as e: return HttpResponseServerError(e.strerror) except Exception as e: return HttpResponseServerError(e) return HttpResponse("The repository has been successfully created") # List retrieve_all repositories if request.method == 'GET': try: # change to the repository directory repositories = Repository.retrieve_all() # remove the .git at the end json_reply = jsonpickle.encode(repositories, unpicklable=False) return HttpResponse(json_reply) except WindowsError as e: return HttpResponseServerError(e.strerror)
def rest_repositorylocation(request): # get the repositories location if request.method == 'GET': repositories_location = Repository.get_location() location = {'repositories': repositories_location} json_reply = jsonpickle.encode(location, unpicklable=False) # send back the location return HttpResponse(json_reply) # modify the repos location if request.method == 'PUT': data = json.loads(request.raw_post_data) repos_location = data['repositories'] if os.path.isdir(repos_location): # check if the last character is a trailing slash if repos_location[-1] != '/': Repository.set_location(repos_location) else: return HttpResponseServerError( "Please make sure to not add any trailing slashes at the end of the directory name." ) return HttpResponse( "The repository location has been updated successfully.") else: return HttpResponseServerError('The directory does not exist')
def rest_repository(request): # Add new repository if request.method == 'POST': name=request.POST['name'] try: # check the repo name matcher = re.compile("^[A-Za-z]\w{2,}$") if matcher.match(name) is None: raise Exception("Please enter an alphanumeric name without spaces") if(name == ""): raise Exception("Please enter a non empty name") # create the repo repository = Repository(name) repository.create() except WindowsError as e: return HttpResponseServerError(e.strerror) except Exception as e: return HttpResponseServerError(e) return HttpResponse("The repository has been successfully created") # List retrieve_all repositories if request.method == 'GET': try: # change to the repository directory repositories = Repository.retrieve_all() # remove the .git at the end json_reply = jsonpickle.encode(repositories, unpicklable = False) return HttpResponse(json_reply) except WindowsError as e: return HttpResponseServerError(e.strerror)
def rest_repositorylocation(request): # get the repositories location if request.method == 'GET': repositories_location = Repository.get_location() location = {'repositories': repositories_location } json_reply = jsonpickle.encode(location, unpicklable = False) # send back the location return HttpResponse(json_reply) # modify the repos location if request.method == 'PUT': data = json.loads(request.raw_post_data) repos_location = data['repositories'] if os.path.isdir(repos_location): # check if the last character is a trailing slash if repos_location[-1] != '/': Repository.set_location(repos_location) else: return HttpResponseServerError("Please make sure to not add any trailing slashes at the end of the directory name.") return HttpResponse("The repository location has been updated successfully.") else: return HttpResponseServerError('The directory does not exist')
def rest_repo_action(request, repo_name): # display repository info if request.method == 'GET': repo = Repository(repo_name) json_reply = jsonpickle.encode(repo, unpicklable=False) return HttpResponse(json_reply) # delete a repo if request.method == 'DELETE': repo = Repository(repo_name) # delete the repo repo.delete() return HttpResponse(repo.name + " has been deleted") if request.method == 'PUT': repo = Repository(repo_name) # retrieve data sent by the client raw_data = json.loads(request.raw_post_data) # retrieve the bare property bare = raw_data['bare'] # if the repository needs to be imported if repo.bare == False and bare == True: # convert the repository repo.convert_to_bare() return HttpResponse("The repository was successfully imported.") return HttpResponseServerError( "The repository was not imported successfully.")
def rest_repo_user_all(request, repo_name): # Get the repository and add the user repo = Repository(repo_name) users = repo.user_list users_name = map(lambda foo: foo.__unicode__(), users) json_reply = json.dumps(users_name) return HttpResponse(json_reply)
def rest_repo_group_all(request, repo_name): # Get the repository and add the user repo = Repository(repo_name) group_list = repo.group_list group__list_names = map(lambda foo: foo.__unicode__(), group_list) json_reply = json.dumps(group__list_names) return HttpResponse(json_reply)
def upgrade(self): from gitstack.models import Repository config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) previous_version = config.get('versionning', 'version') if previous_version == "1.5": # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings config.set('versionning', 'version', '2.0') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() if previous_version == "1.4": # write a config file with the version number # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings config.set('versionning', 'version', '1.5') # add the protocol section config.add_section('protocols') config.set('protocols', 'http', 'true') config.set('protocols', 'https', 'false') config.set('protocols', 'httpport', '80') config.set('protocols', 'httpsport', '443') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() # create a group file if it does not exist if not os.path.isfile(settings.GROUP_FILE_PATH): # create an empty group file group_file = open(settings.GROUP_FILE_PATH, 'w') group_file.write('') group_file.close() # copy the self signed certificates self.copy_certificates() # write a config for each repo repo_list = Repository.retrieve_all() for repo in repo_list: repo.load() repo.save() # upgrade now to 2.0 self.upgrade()
def rest_repo_action(request, repo_name): # display repository info if request.method == 'GET': repo = Repository(repo_name) json_reply = jsonpickle.encode(repo, unpicklable = False) return HttpResponse(json_reply) # delete a repo if request.method == 'DELETE': repo = Repository(repo_name) # delete the repo repo.delete() return HttpResponse(repo.name + " has been deleted") if request.method == 'PUT': repo = Repository(repo_name) # retrieve data sent by the client raw_data = json.loads(request.raw_post_data) # retrieve the bare property bare = raw_data['bare'] # if the repository needs to be imported if repo.bare == False and bare == True: # convert the repository repo.convert_to_bare() return HttpResponse("The repository was successfully imported.") return HttpResponseServerError("The repository was not imported successfully.")
def rest_settings_authentication(request): # Get the settings if request.method == 'GET': # load the settings file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # retrieve the settings ldap_helper = LdapHelper() # build json reply # if the ldap password has a value if ldap_helper.bind_password != '': # write "saved" instead of the password displayed_bind_password = "******" json_reply = '{"authMethod":"' + ldap_helper.auth_method + '","ldap":{"protocol": "' + ldap_helper.protocol +'","host": "' + ldap_helper.host +'","port": "' + ldap_helper.port +'","baseDn": "' + ldap_helper.base_dn +'","attribute": "' + ldap_helper.attribute +'","scope": "' + ldap_helper.scope +'","filter": "' + ldap_helper.filter +'","bindDn": "' + ldap_helper.bind_dn +'","bindPassword": "******"}}' # json_reply = '{"authMethod":"' + auth_method + '","ldap":{"host": "' + ldap_host +'","baseDn": "' + ldap_base_dn +'","bindDn": "' + ldap_bind_dn +'","bindPassword": "******"}}' # json_reply = '{"authMethod":"ldap","ldap":{"url": "ldap://10.0.1.24:389/","baseDn": "CN=Users,DC=contoso,DC=com","bindDn": "CN=john,CN=Users,DC=contoso,DC=com","bindPassword": "******"}}' return HttpResponse(json_reply) # Set the settings if request.method == 'PUT': auth_settings = json.loads(request.raw_post_data) # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings # retrieve the settings ldap_helper = LdapHelper() ldap_helper.auth_method = auth_settings['authMethod'] ldap_helper.protocol = auth_settings['ldap']['protocol'] ldap_helper.host = auth_settings['ldap']['host'] ldap_helper.port = auth_settings['ldap']['port'] ldap_helper.base_dn = auth_settings['ldap']['baseDn'] ldap_helper.attribute = auth_settings['ldap']['attribute'] ldap_helper.scope = auth_settings['ldap']['scope'] ldap_helper.filter = auth_settings['ldap']['filter'] ldap_helper.bind_dn = auth_settings['ldap']['bindDn'] if auth_settings['ldap']['bindPassword'] != "saved": ldap_helper.bind_password = auth_settings['ldap']['bindPassword'] ldap_helper.save() # Load and save all the repositories to update the ldap config repositories = Repository.retrieve_all() for repository in repositories: repository.load() repository.save() return HttpResponse("Settings successfully saved.")
def rest_settings_authentication(request): # Get the settings if request.method == 'GET': # load the settings file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # retrieve the settings ldap_helper = LdapHelper() # build json reply # if the ldap password has a value if ldap_helper.bind_password != '': # write "saved" instead of the password displayed_bind_password = "******" json_reply = '{"authMethod":"' + ldap_helper.auth_method + '","ldap":{"protocol": "' + ldap_helper.protocol + '","host": "' + ldap_helper.host + '","port": "' + ldap_helper.port + '","baseDn": "' + ldap_helper.base_dn + '","attribute": "' + ldap_helper.attribute + '","scope": "' + ldap_helper.scope + '","filter": "' + ldap_helper.filter + '","bindDn": "' + ldap_helper.bind_dn + '","bindPassword": "******"}}' # json_reply = '{"authMethod":"' + auth_method + '","ldap":{"host": "' + ldap_host +'","baseDn": "' + ldap_base_dn +'","bindDn": "' + ldap_bind_dn +'","bindPassword": "******"}}' # json_reply = '{"authMethod":"ldap","ldap":{"url": "ldap://10.0.1.24:389/","baseDn": "CN=Users,DC=contoso,DC=com","bindDn": "CN=john,CN=Users,DC=contoso,DC=com","bindPassword": "******"}}' return HttpResponse(json_reply) # Set the settings if request.method == 'PUT': auth_settings = json.loads(request.raw_post_data) # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings # retrieve the settings ldap_helper = LdapHelper() ldap_helper.auth_method = auth_settings['authMethod'] ldap_helper.protocol = auth_settings['ldap']['protocol'] ldap_helper.host = auth_settings['ldap']['host'] ldap_helper.port = auth_settings['ldap']['port'] ldap_helper.base_dn = auth_settings['ldap']['baseDn'] ldap_helper.attribute = auth_settings['ldap']['attribute'] ldap_helper.scope = auth_settings['ldap']['scope'] ldap_helper.filter = auth_settings['ldap']['filter'] ldap_helper.bind_dn = auth_settings['ldap']['bindDn'] if auth_settings['ldap']['bindPassword'] != "saved": ldap_helper.bind_password = auth_settings['ldap']['bindPassword'] ldap_helper.save() # Load and save all the repositories to update the ldap config repositories = Repository.retrieve_all() for repository in repositories: repository.load() repository.save() return HttpResponse("Settings successfully saved.")
def add_repo_user_dialog(request, repo_name): # retrieve all the users user_list = UserFactory.instantiate_user('').retrieve_all() # get the users already added to the repository repository = Repository(repo_name) repository_user_list = repository.user_list # substract the repository users from the user list for repository_user in repository_user_list: user_list.remove(repository_user) return render_to_response('gitstack/add_repo_user.html', { 'repo_name': repo_name, 'user_list': user_list }, context_instance=RequestContext(request))
def add_repo_group_dialog(request, repo_name): # retrieve all the users group_list = Group.retrieve_all() # get the users already added to the repository repository = Repository(repo_name) repository_group_list = repository.group_list # substract the repository groups from the group list for repository_group in repository_group_list: if repository_group in group_list: group_list.remove(repository_group) return render_to_response('gitstack/add_repo_group.html', { 'repo_name': repo_name, 'group_list': group_list }, context_instance=RequestContext(request))
def tearDown(self): # delete repos repositories = Repository.retrieve_all() for repo in repositories: repo.delete() # delete users os.remove(settings.INSTALL_DIR + '/data/passwdfile') # delete groups groups = Group.retrieve_all() for group in groups: # delete the group group.delete() # remove the settings.ini file from the filesystem os.remove(settings.SETTINGS_PATH)
def tearDown(self): # delete repos repositories = Repository.retrieve_all() for repo in repositories: repo.delete() # delete users users = UserApache.retrieve_all() for user in users: # delete the user if user.username != 'everyone': user.delete() time.sleep(0.1) # delete groups groups = Group.retrieve_all() for group in groups: # delete the group group.delete()
def rest_repo_group(request, repo_name, group_name): repo = Repository(repo_name) group = Group(group_name) # Add group if request.method == 'POST': # Get the repository and add the user repo.add_group(group) repo.add_group_read(group) repo.add_group_write(group) repo.save() return HttpResponse("User " + group_name + " added to " + repo_name) # Delete the group if request.method == 'DELETE': # Remove the group from the repository repo.remove_group(group) repo.save() return HttpResponse(group_name + " removed from " + repo_name) # Get the group permissions if request.method == 'GET': permissions = {'read' : False, 'write' : False} # retrieve the list of read and write users group_read_list = repo.group_read_list group_write_list = repo.group_write_list # check if the user has read and write access if group in group_read_list: permissions['read'] = True if group in group_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_group_read(group) else: repo.remove_group_read(group) if 'write' in permissions: # add the write permission to the repo if permissions['write']: repo.add_group_write(group) else: repo.remove_group_write(group) repo.save() return HttpResponse(group_name + "'s permissions updated")
def rest_repo_user(request, repo_name, username): repo = Repository(repo_name) user = UserFactory.instantiate_user(username) # Add user if request.method == 'POST': # 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) # 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")
def upgrade(self): from gitstack.models import Repository config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) previous_version = config.get('versionning', 'version') if previous_version == "2.2": # upgrade to 2.3 open(settings.INSTALL_DIR + '/data/core', 'w').close() # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) config.set('versionning', 'version', '2.3') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() if previous_version == "2.1": # upgrade to 2.2 open(settings.INSTALL_DIR + '/data/core', 'w').close() # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) config.set('versionning', 'version', '2.2') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() if previous_version == "2.0": # upgrade to 2.1 # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # create the section location and add a default location config.add_section('location') config.set('location', 'repositories', settings.INSTALL_DIR + '/repositories') config.set('versionning', 'version', '2.1') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() if previous_version == "1.5": # upgrade to 2.0 # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings config.set('versionning', 'version', '2.0') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() # upgrade to 2.1 self.upgrade() if previous_version == "1.4": # upgrade to 1.5 # write a config file with the version number # load the config file config = ConfigParser.ConfigParser() config.read(settings.SETTINGS_PATH) # save the settings config.set('versionning', 'version', '1.5') # add the protocol section config.add_section('protocols') config.set('protocols', 'http', 'true') config.set('protocols', 'https', 'false') config.set('protocols', 'httpport', '80') config.set('protocols', 'httpsport', '443') f = open(settings.SETTINGS_PATH, "w") config.write(f) f.close() # create a group file if it does not exist if not os.path.isfile(settings.GROUP_FILE_PATH): # create an empty group file group_file = open(settings.GROUP_FILE_PATH, 'w') group_file.write('') group_file.close() # copy the self signed certificates self.copy_certificates() # write a config for each repo repo_list = Repository.retrieve_all() for repo in repo_list: repo.load() repo.save() # upgrade now to 2.0 self.upgrade()
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")
def rest_repo_group(request, repo_name, group_name): repo = Repository(repo_name) group = Group(group_name) # Add group if request.method == 'POST': # Get the repository and add the user repo.add_group(group) repo.add_group_read(group) repo.add_group_write(group) repo.save() return HttpResponse("User " + group_name + " added to " + repo_name) # Delete the group if request.method == 'DELETE': # Remove the group from the repository repo.remove_group(group) repo.save() return HttpResponse(group_name + " removed from " + repo_name) # Get the group permissions if request.method == 'GET': permissions = {'read': False, 'write': False} # retrieve the list of read and write users group_read_list = repo.group_read_list group_write_list = repo.group_write_list # check if the user has read and write access if group in group_read_list: permissions['read'] = True if group in group_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_group_read(group) else: repo.remove_group_read(group) if 'write' in permissions: # add the write permission to the repo if permissions['write']: repo.add_group_write(group) else: repo.remove_group_write(group) repo.save() return HttpResponse(group_name + "'s permissions updated")