function add_source_gitolite_repository() { $is_gitolite = GitoliteAdmin::is_gitolite(); $logged_user = $this->logged_user; $user_id = $logged_user->getId(); $no_key_warning = FALSE; $view_url = ""; if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'ac_gitolite_source') { $do_continue = true; } if ($do_continue) { // Add Administrator , Leaders and Project Manager in allowed people list //$role = new Roles(); //$admins = $role::findAdministrators(); $usrobj = new Users(); $users_details = $usrobj->findAdministrators(); if (is_foreachable($users_details)) { foreach ($users_details as $key => $value) { // check key exists $user_keys = GitoliteAc::check_keys_added($value->getId()); if ($user_keys > 0) { $user_detail_permissions[$value->getId()] = array('readaccess' => 0, 'writeaccess' => 0, 'writeaccessplus' => 1, 'user_keys' => $user_keys); $allowed_users[$value->getId()] = $value->getDisplayName(); } } } $this->response->assign(array('curr_users' => $allowed_users, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('add_source_gitolite_repository'), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'view_url' => $view_url)); } else { $this->response->assign(array('add_error' => TRUE)); } if ($this->request->isSubmitted()) { // check for form submission try { /* Check form with validation error */ $repository_data = $this->request->post('repository'); if (!isset($repository_data["repo_notification_setting"])) { $repository_data["repo_notification_setting"] = "no"; } $errors = new ValidationErrors(); $post_data = $this->request->post(); $settings = GitoliteAdmin::get_admin_settings(); $is_remote = !isset($settings["git_server_location"]) || $settings["git_server_location"] != "remote" ? false : true; if (!$is_remote) { $sever_user_path = GitoliteAdmin::get_server_user_path(); if (!$sever_user_path) { $errors->addError('Repository path on server invalid'); } } $repo_name = trim($repository_data['name']); $access = $post_data['access']; if ($repo_name == "") { $errors->addError('Please enter repository name', 'repo_name'); } if (!is_array($access) && count($access) == 0) { $errors->addError('Select access levels for user', 'access'); } /* Check for duplications repository name and Key */ if (!$errors->hasErrors()) { if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) { $errors->addError('Please enter valid repository name.', 'repo_name'); } $dup_cnt = ProjectGitolite::check_source_git_dup($repository_data); if (count($dup_cnt) == 0) { $errors->addError('Problem occured while saving data, please try again.'); } elseif (count($dup_cnt) > 0) { if ($dup_cnt[0]['dup_name_cnt'] > 0) { $errors->addError('Repository name already used'); } } } // if errors found throw error exception if ($errors->hasErrors()) { throw $errors; } /** save gitolite details in database * */ // save reponame try { DB::beginWork('Creating a new repository @ ' . __CLASS__); /** * if gitolite is setup on remote, change repo path */ if (!$is_remote) { $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git"; } elseif ($is_remote) { $repo_path = GIT_FILES_PATH . "/" . $repo_name; } if (is_array($post_data)) { $repository_path_url = array('repository_path_url' => $repo_path); } $repository_data = array_merge($repository_data, $repository_path_url); $clone_url = $settings['git_clone_url'] . $repo_name; $this->active_repository = new GitRepository(); $this->active_repository->setAttributes($repository_data); $this->active_repository->setCreatedBy($this->logged_user); $this->active_repository->save(); $repo_fk = $this->active_repository->getId(); if ($repo_fk) { $repo_id = ProjectGitolite::add_repo_details($repo_fk, 0, $user_id, $repo_path, $repository_data, $clone_url); if ($repo_id) { $add_access = ProjectGitolite::add_access_levels($repo_id, serialize($post_data['access']), $user_id, 1); if ($add_access) { $res = ProjectGitolite::render_conf_file(); $dir = $settings['gitoliteadminpath'] . "gitolite-admin"; $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push || echo 'Not found'"; exec($command, $output, $return_var); if ($is_remote) { $git_server = $settings['gitoliteuser'] . "@" . $settings['gitoliteserveradd']; //$command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name; chdir(GIT_FILES_PATH); $command = "git clone " . $git_server . ":" . $repo_name; exec($command, $output, $return_var); } } else { $errors->addError('Error while saving access levels.'); throw $errors; } } else { $errors->addError('Error while saving repository.'); throw $errors; } } else { $errors->addError('Error while saving repository.'); throw $errors; } DB::commit('Repository created @ ' . __CLASS__); $this->response->respondWithData($this->active_repository, array('as' => 'repository')); } catch (Exception $e) { DB::rollback('Failed to create a repository @ ' . __CLASS__); $this->response->exception($e); } } catch (Exception $e) { DB::rollback('Failed to create a repository @ ' . __CLASS__); $this->response->exception($e); } } }
/** * Render conf file once mapping is done * @return string message */ function render_after_clone_conf() { $res = ProjectGitolite::render_conf_file(); $settings = GitoliteAdmin::get_admin_settings(); $dir = $settings['gitoliteadminpath'] . "gitolite-admin"; $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push || echo 'Not found'"; exec($command, $output, $return_var); //print_r($output); die("ok"); }
/** * Edit gitolite repository access levels * @throws ValidationErrors */ function edit_git_repo() { $repo_id = array_var($_GET, 'project_source_repository_id'); //project objects id $is_gitolite = GitoliteAdmin::is_gitolite(); if (!ProjectSourceRepositories::canAdd($this->logged_user, $this->active_project)) { $this->response->forbidden(); } // if $project = $this->active_project; $project_id = $project->getId(); $logged_user = $this->logged_user; $user_id = $logged_user->getId(); $no_key_warning = FALSE; // to give warning if logged in user has not added his public key if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'project_tracking_gitolite') { $do_continue = true; } if ($do_continue) { $users_details = $this->active_project->users()->describe($this->logged_user, true, true, STATE_VISIBLE); $repo_details = ProjectGitolite::get_repo_details($repo_id); /* print_r($repo_details); die(); */ $repository_data = $this->request->post('repository'); if (!is_array($repository_data)) { $repository_data = array('updatetype' => $this->active_repository->getFieldValue("update_type"), 'visibility' => $this->project_object_repository->getVisibility()); } // if if (is_array($repo_details) && count($repo_details) > 0) { // repository id from integer_field_1 in project_objects , we are saving this id in our tables. $git_repo_id = $repo_details['repo_id']; $access_array = ProjectGitolite::get_access_levels($git_repo_id); //$result_access = DB::execute("SELECT * from $access_table_name where repo_id = '".$repo_details['repo_id']."'"); if (is_array($access_array) && count($access_array) > 0) { $access = $access_array['permissions']; $permissions = @unserialize($access); if ($permissions !== false || $permissions === 'b:0;') { $permissions_array = $permissions; } else { $permissions_array = array(); } } else { $this->response->forbidden(); } } else { $this->response->forbidden(); } //print_r($permissions_array); $user_detail_permissions = array(); $view_url = false; if (is_foreachable($users_details)) { foreach ($users_details as $key => $value) { // check key exists $user_keys = GitoliteAc::check_keys_added($value['user']['id']); if ($user_keys > 0) { $user_detail_permissions[$value['user']['id']] = array('readaccess' => $permissions_array[$value['user']['id']] == "2" ? TRUE : FALSE, 'writeaccess' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'writeaccessplus' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'user_keys' => $user_keys); $allowed_users[$value['user']['id']] = $value['user']['name']; } } } if (!isset($user_detail_permissions[$user_id]) && ($this->logged_user->isAdministrator() || $this->logged_user->isProjectManager())) { $repoobj = new ProjectSourceRepositories(); $objuser = new rtmUser($user_id); $user_keys = GitoliteAc::check_keys_added($user_id); if ($user_keys) { $user_detail_permissions[$user_id] = array('readaccess' => $permissions_array[$user_id] == "2" ? TRUE : $repoobj->canAccess($objuser, $project), 'writeaccess' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canAdd($objuser, $project), 'writeaccessplus' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canManage($objuser, $project), 'user_keys' => $user_keys); $allowed_users[$user_id] = $logged_user->getName(); } else { $no_key_warning = TRUE; $view_url = $this->logged_user->getViewUrl(); } } $this->response->assign(array('curr_users' => $allowed_users, 'repo_details' => $repo_details, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('edit_git_repository', array('project_slug' => $project->getSlug(), 'project_source_repository_id' => $repo_id)), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'repository_data' => $repository_data, 'view_url' => $view_url)); } else { $this->response->assign(array('add_error' => TRUE)); } if ($this->request->isSubmitted()) { // check for form submission try { /* Check form with validation error */ $repository_data = $this->request->post('repository'); $errors = new ValidationErrors(); $post_data = $this->request->post(); $settings = GitoliteAdmin::get_admin_settings(); $sever_user_path = GitoliteAdmin::get_server_user_path(); if (!$sever_user_path) { // $errors->addError ( 'Repository path on server invalid' ); } $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git"; $repo_name = trim($repository_data['name']); $access = $post_data['access']; if ($repo_name == "") { $errors->addError('Please enter repository name', 'repo_name'); } if (!is_array($access) && count($access) == 0) { $errors->addError('Select access levels for user', 'access'); } /* Check for duplications repository name and Key */ if (!$errors->hasErrors()) { if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) { $errors->addError('Please enter valid repository name.', 'repo_name'); } /* $dup_cnt = ProjectGitolite::check_duplication($project_id,$repository_data); if(count($dup_cnt) == 0) { $errors->addError('Problem occured while saving data, please try again.'); } elseif(count($dup_cnt) > 0) { if($dup_cnt[0]['dup_name_cnt'] > 0) { $errors->addError('You have already added repository with same name.'); } } */ } // if errors found throw error exception if ($errors->hasErrors()) { throw $errors; } /** save gitolite details in database * */ // save reponame try { DB::beginWork('Update repository @ ' . __CLASS__); $this->active_repository->setAttributes($repository_data); $this->project_object_repository->setVisibility($repository_data['visibility']); $this->project_object_repository->setName($repository_data['name']); $this->active_repository->save(); $this->project_object_repository->save(); $repo_fk = $this->active_repository->getId(); if ($repo_id) { $notif_setting = isset($repository_data["disable_notifications"]) ? "yes" : "no"; $repo_table_name = TABLE_PREFIX . "rt_gitolite_repomaster"; DB::execute("update {$repo_table_name} set disable_notifications = '" . $notif_setting . "' where repo_fk = '" . $repo_fk . "'"); $update_access = ProjectGitolite::update_access_levels($git_repo_id, serialize($post_data['access'])); if ($update_access) { $res = ProjectGitolite::render_conf_file(); $dir = $settings['gitoliteadminpath'] . "gitolite-admin"; $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push || echo 'Not found'"; exec($command, $output, $return_var); /* $git_server = $settings['gitoliteuser']."@".$settings['gitoliteserveradd']; $command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name; exec($command,$output,$return_var); */ } else { $errors->addError('Error while saving access levels.'); throw $errors; } } else { $errors->addError('Error while saving repository.'); throw $errors; } DB::commit('Repository created @ ' . __CLASS__); $this->response->ok(); } catch (Exception $e) { DB::rollback('Failed to create a repository @ ' . __CLASS__); $this->response->exception($e); } } catch (Exception $e) { DB::rollback('Failed to create a repository @ ' . __CLASS__); $this->response->exception($e); } } }
/** * Add new public key of user. Create .pub file gitolite admin directory * @return void */ function add_public_keys() { $active_user = $this->active_user; $this->response->assign(array('form_action' => Router::assemble('add_public_keys', array('company_id' => $active_user->getCompanyId(), 'user_id' => $active_user->getId())), 'user_rmail' => $active_user->getEmail())); if ($this->request->isSubmitted()) { // check for form submission $post_data = $this->request->post(); /* Check form with validation error */ $errors = new ValidationErrors(); try { $post_data['key_name'] = str_replace(array("\r\n", "\r", "\n"), "", $post_data['key_name']); $post_data['public_keys'] = str_replace(array("\r\n", "\r", "\n"), "", $post_data['public_keys']); $key_name = trim($post_data['key_name']); $public_keys = trim($post_data['public_keys']); if ($key_name == "") { $errors->addError('Please enter key name', 'key_name'); } if ($public_keys == "") { $errors->addError('Please enter key', 'public_keys'); } if (!preg_match("/^[A-Za-z0-9-]+\$/", $key_name)) { $errors->addError('Please enter valid key name.', 'public_keys'); } $fetch_actual_key = explode(" ", $public_keys); if (!($fetch_actual_key[0] == "ssh-rsa" || $fetch_actual_key[0] == "ssh-dss")) { $errors->addError("Key is invalid. It must begin with 'ssh-rsa' or 'ssh-dss'. Check that you're copying the public half of the key", 'public_keys'); } else { $tempStr = base64_decode($fetch_actual_key[1], true); if ($tempStr) { if (strpos($tempStr, $fetch_actual_key[0]) === false) { $errors->addError("Key is invalid. Check that you're copying the public half of the key", 'public_keys'); } } else { $errors->addError("Key is invalid. Check that you're copying the public half of the key", 'public_keys'); } } $actual_key = $fetch_actual_key[1]; /* Check for duplications Key name and Key */ if (!$errors->hasErrors()) { $dup_cnt = GitoliteAc::check_duplication($active_user->getId(), $post_data, $actual_key); if (count($dup_cnt) == 0) { $errors->addError('Problem occured while saving data, please try again.', 'public_keys'); } elseif (count($dup_cnt) > 0) { if ($dup_cnt[0]['dup_name_cnt'] > 0) { $errors->addError('You have already added key with same name.'); } if ($dup_cnt[1]['dup_name_cnt'] > 0) { $errors->addError('Entered key is already added.'); } } } // if errors found throw error exception if ($errors->hasErrors()) { throw $errors; } } catch (Exception $e) { $this->response->exception($e); } // insert key details in database. $pub_file_name = $key_name . "-" . $this->request->get("user_id"); try { DB::beginWork('Adding a new public key @ ' . __CLASS__); //print_r($post_data); $save_data = GitoliteAc::add_keys($active_user->getId(), $pub_file_name, $post_data); if ($save_data) { $file = $pub_file_name . ".pub"; $admin_settings = GitoliteAdmin::get_admin_settings(); if (!isset($admin_settings['gitoliteadminpath'])) { $this->response->exception("Gitolite admin path not set"); die; } $dirpath = $admin_settings['gitoliteadminpath'] . "gitolite-admin/keydir/"; $adminrepo = $admin_settings['gitoliteadminpath'] . "gitolite-admin/"; $path = $dirpath . $file; $newfh = fopen($path, 'w+'); if (!is_writable($path)) { $this->response->exception("Can't write to file public file"); die; } $res = fwrite($newfh, $post_data['public_keys']); fclose($fh); ProjectGitolite::update_repo_conf_on_public_key($active_user->getId()); $res = ProjectGitolite::render_conf_file(); /** Git Push Files * */ $command = "cd " . $adminrepo . " && git add * && git commit -am 'added key for user {$file}' && git push"; exec($command, $output, $return_var); //cd /var/www/vhosts/web-vision.de/httpdocs/project/work/git/gitolite/gitolite-admin/ && git add * && git commit -am 'added key for user faishal-test-key-147.pub' && git push DB::commit('Key added @ ' . __CLASS__); $show_data['key_name'] = $post_data['key_name']; $show_data['public_key'] = substr($post_data['public_keys'], 0, 25) . "....." . substr($post_data['public_keys'], -30); $show_data['delete_url'] = $this->active_user->getViewUrl() . "/" . "delete-keys" . "/" . $save_data; $this->response->respondWithData($show_data, array('as' => 'settings')); } } catch (Exception $e) { $this->response->exception("Can't save key this time, might be key you are adding is already added"); } } }