-
Notifications
You must be signed in to change notification settings - Fork 2
/
project_git_auth.module
189 lines (178 loc) · 6.5 KB
/
project_git_auth.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
// $Id$
/**
* @file
* Creates repositories and grants access for projects.
*/
/**
* Implementation of hook_menu().
*/
function project_git_auth_menu() {
$items = array();
$items['project-git-auth'] = array(
'page title' => 'Git Authentication Service',
'page callback' => 'project_git_auth_service',
'access arguments' => array('view projects of maintainer'),
);
$items['admin/settings/project-git-auth'] = array(
'page title' => 'Git Reposiotry Manager Configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('project_git_auth_settings_form'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Provide the settings form.
*/
function project_git_auth_settings_form() {
$form['project_git_auth_beanstalk_url'] = array(
'#title' => t('Beanstalkd URL'),
'#type' => 'textfield',
'#description' => t('The URL (or IP address) of your Beanstalkd Daemon used to queue repository actions.'),
'#default_value' => variable_get('project_git_auth_beanstalk_url', '127.0.0.1'),
);
$form['project_git_auth_beanstalk_port'] = array(
'#title' => t('Beanstalkd Port'),
'#type' => 'textfield',
'#description' => t('The port number on which your Beanstalkd Daemon is listening.'),
'#default_value' => variable_get('project_git_auth_beanstalk_port', '11300'),
);
$form['project_git_auth_repository_base_path'] = array(
'#title' => t('Base Repository Path'),
'#type' => 'textfield',
'#description' => t('The path on disk where this repository resides.'),
'#default_value' => variable_get('project_git_auth_repository_base_path', '/git'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_perm().
*/
function project_git_auth_perm() {
return array(
'view projects of maintainer',
);
}
/**
* Implementation of hook_nodeapi(
*/
function project_git_auth_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$path = drupal_get_path('module', 'project_git_auth') . '/pheanstalk/pheanstalk_init.php';
if (isset($node->type) && $node->type == 'project_project' && file_exists($path)) {
switch ($op) {
case 'insert':
case 'delete':
// Create the job for our python daemon.
$data = array(
'project' => $node->project['uri'],
);
$repo_root = variable_get('project_git_auth_repository_base_path', '/git') . '/' . $node->project['uri'] . '.git';
if ($op == 'insert') {
if (module_exists('versioncontrol_git')) {
// Setup the repository data.
$data['name'] = $node->title;
$data['vcs'] = 'git';
$data['backend'] = 'VersionControlGitBackend';
$data['root'] = $repo_root;
// Create and save versioncontrol entity
$repository = new VersioncontrolGitRepository();
$repository->build($data);
$repository->insert();
}
drupal_set_message(t('Your git repository (%repo) has been queued for creation and should be available almost immediately.', array('%repo' => $node->project['uri'] . '.git')));
$data['operation'] = 'create';
}
elseif ($op == 'delete') {
drupal_set_message(t('Your git repository (%repo) will be deleted.', array('%repo' => $node->project['uri'] . '.git')));
$data['operation'] = 'delete';
if (module_exists('versioncontrol_git')) {
// TODO: Is there a better way to find a repository?
// Do we want to relate repositories to projects in a more robust way than by magical formula around uri?
$data['repo_id'] = db_result(db_query("SELECT repo_id from {versioncontrol_repositories} WHERE root='%s'", $repo_root));
$repository = new VersioncontrolGitRepository();
$repository->build($data);
$repository->delete();
}
}
project_git_auth_create_reposiotry_job($data);
break;
}
}
}
/**
* Create a job in the Beanstalkd job.
*
* @param $data
* An associative array to be encoded into json data and added to the beanstalkd job queue.
*/
function project_git_auth_create_reposiotry_job($data) {
// Register Pheanstalk class loader
require_once(drupal_get_path('module', 'project_git_auth') . '/pheanstalk/pheanstalk_init.php');
// TODO: if we were actually going to use this we'd need to make beanstalksd's location configurable.
$pheanstalk = new Pheanstalk(variable_get('project_git_auth_beanstalk_url', '127.0.0.1'), variable_get('project_git_auth_beanstalk_port', '11300'));
$pheanstalk
->put(json_encode($data));
}
/**
* Return a list of the repositories a user has access to.
*
* @param $uid
* The {user}.uid to retrieve repositories for.
*
* @return
* A linear array of repository names (currently just project short names).
*/
function project_git_auth_get_repos($uid) {
$sql = "SELECT uri FROM {project_projects} pp
JOIN {project_maintainer} pm ON pp.nid = pm.nid
WHERE pm.uid=%d";
$result = db_query($sql, $uid);
$repositories = array();
while ($project = db_result($result)) {
$repositories[] = $project;
}
return $repositories;
}
/**
* The menu callback for the authenticator service.
*/
function project_git_auth_service() {
module_load_include('inc', 'sshkey');
// We use the HTTP GET request method for caching reasons.
if ($_GET['user']) {
$username = urldecode($_GET['user']);
try {
$account = user_load(array('name' => check_plain($username)));
$uid = $account->uid;
$password = $account->pass;
$keys = sshkey_user_public_keys_list($uid);
foreach ($keys as &$key) {
$key = $key["fingerprint"];
}
$repos = project_git_auth_get_repos($uid);
$data = array("keys" => $keys, "password" => $password, "repos" => $repos);
}
catch (Exception $e) {
$uid = 0;
$data = array("keys" => array(), "password" => "", "repos" => array());
}
drupal_alter('project_git_auth_user_data', $data, $uid);
drupal_json($data);
}
elseif ($_GET['fingerprint']) {
$fingerprint = urldecode($_GET['fingerprint']);
try {
$fingerprint = str_replace(':', '', $fingerprint);
$loaded_key = sshkey_public_key_load($fingerprint);
$uid = $loaded_key['uid'];
$repositories = project_git_auth_get_repos($uid);
}
catch (Exception $e) {
$repositories = array();
$uid = 0;
}
drupal_alter('project_git_auth_user_repositories', $repositories, $uid);
drupal_json($repositories);
}
}