Show More
@@ -1,571 +1,602 b'' | |||||
1 | .. _api: |
|
1 | .. _api: | |
2 |
|
2 | |||
3 |
|
3 | |||
4 | API |
|
4 | API | |
5 | === |
|
5 | === | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | Starting from RhodeCode version 1.2 a simple API was implemented. |
|
8 | Starting from RhodeCode version 1.2 a simple API was implemented. | |
9 | There's a single schema for calling all api methods. API is implemented |
|
9 | There's a single schema for calling all api methods. API is implemented | |
10 | with JSON protocol both ways. An url to send API request in RhodeCode is |
|
10 | with JSON protocol both ways. An url to send API request in RhodeCode is | |
11 | <your_server>/_admin/api |
|
11 | <your_server>/_admin/api | |
12 |
|
12 | |||
13 | API ACCESS FOR WEB VIEWS |
|
13 | API ACCESS FOR WEB VIEWS | |
14 | ++++++++++++++++++++++++ |
|
14 | ++++++++++++++++++++++++ | |
15 |
|
15 | |||
16 | API access can also be turned on for each web view in RhodeCode that is |
|
16 | API access can also be turned on for each web view in RhodeCode that is | |
17 | decorated with `@LoginRequired` decorator. To enable API access simple change |
|
17 | decorated with `@LoginRequired` decorator. To enable API access simple change | |
18 | the standard login decorator to `@LoginRequired(api_access=True)`. |
|
18 | the standard login decorator to `@LoginRequired(api_access=True)`. | |
19 | After this change, a rhodecode view can be accessed without login by adding a |
|
19 | After this change, a rhodecode view can be accessed without login by adding a | |
20 | GET parameter `?api_key=<api_key>` to url. By default this is only |
|
20 | GET parameter `?api_key=<api_key>` to url. By default this is only | |
21 | enabled on RSS/ATOM feed views. |
|
21 | enabled on RSS/ATOM feed views. | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | API ACCESS |
|
24 | API ACCESS | |
25 | ++++++++++ |
|
25 | ++++++++++ | |
26 |
|
26 | |||
27 | All clients are required to send JSON-RPC spec JSON data:: |
|
27 | All clients are required to send JSON-RPC spec JSON data:: | |
28 |
|
28 | |||
29 | { |
|
29 | { | |
30 | "id:<id>, |
|
30 | "id:<id>, | |
31 | "api_key":"<api_key>", |
|
31 | "api_key":"<api_key>", | |
32 | "method":"<method_name>", |
|
32 | "method":"<method_name>", | |
33 | "args":{"<arg_key>":"<arg_val>"} |
|
33 | "args":{"<arg_key>":"<arg_val>"} | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | Example call for autopulling remotes repos using curl:: |
|
36 | Example call for autopulling remotes repos using curl:: | |
37 | curl https://server.com/_admin/api -X POST -H 'content-type:text/plain' --data-binary '{"id":1,"api_key":"xe7cdb2v278e4evbdf5vs04v832v0efvcbcve4a3","method":"pull","args":{"repo":"CPython"}}' |
|
37 | curl https://server.com/_admin/api -X POST -H 'content-type:text/plain' --data-binary '{"id":1,"api_key":"xe7cdb2v278e4evbdf5vs04v832v0efvcbcve4a3","method":"pull","args":{"repo":"CPython"}}' | |
38 |
|
38 | |||
39 | Simply provide |
|
39 | Simply provide | |
40 | - *id* A value of any type, which is used to match the response with the request that it is replying to. |
|
40 | - *id* A value of any type, which is used to match the response with the request that it is replying to. | |
41 | - *api_key* for access and permission validation. |
|
41 | - *api_key* for access and permission validation. | |
42 | - *method* is name of method to call |
|
42 | - *method* is name of method to call | |
43 | - *args* is an key:value list of arguments to pass to method |
|
43 | - *args* is an key:value list of arguments to pass to method | |
44 |
|
44 | |||
45 | .. note:: |
|
45 | .. note:: | |
46 |
|
46 | |||
47 | api_key can be found in your user account page |
|
47 | api_key can be found in your user account page | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | RhodeCode API will return always a JSON-RPC response:: |
|
50 | RhodeCode API will return always a JSON-RPC response:: | |
51 |
|
51 | |||
52 | { |
|
52 | { | |
53 | "id":<id>, |
|
53 | "id":<id>, | |
54 | "result": "<result>", |
|
54 | "result": "<result>", | |
55 | "error": null |
|
55 | "error": null | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | All responses from API will be `HTTP/1.0 200 OK`, if there's an error while |
|
58 | All responses from API will be `HTTP/1.0 200 OK`, if there's an error while | |
59 | calling api *error* key from response will contain failure description |
|
59 | calling api *error* key from response will contain failure description | |
60 | and result will be null. |
|
60 | and result will be null. | |
61 |
|
61 | |||
62 | API METHODS |
|
62 | API METHODS | |
63 | +++++++++++ |
|
63 | +++++++++++ | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | pull |
|
66 | pull | |
67 | ---- |
|
67 | ---- | |
68 |
|
68 | |||
69 | Pulls given repo from remote location. Can be used to automatically keep |
|
69 | Pulls given repo from remote location. Can be used to automatically keep | |
70 | remote repos up to date. This command can be executed only using api_key |
|
70 | remote repos up to date. This command can be executed only using api_key | |
71 | belonging to user with admin rights |
|
71 | belonging to user with admin rights | |
72 |
|
72 | |||
73 | INPUT:: |
|
73 | INPUT:: | |
74 |
|
74 | |||
75 | api_key : "<api_key>" |
|
75 | api_key : "<api_key>" | |
76 | method : "pull" |
|
76 | method : "pull" | |
77 | args : { |
|
77 | args : { | |
78 | "repo_name" : "<reponame>" |
|
78 | "repo_name" : "<reponame>" | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | OUTPUT:: |
|
81 | OUTPUT:: | |
82 |
|
82 | |||
83 | result : "Pulled from <reponame>" |
|
83 | result : "Pulled from <reponame>" | |
84 | error : null |
|
84 | error : null | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | get_user |
|
87 | get_user | |
88 | -------- |
|
88 | -------- | |
89 |
|
89 | |||
90 | Get's an user by username, Returns empty result if user is not found. |
|
90 | Get's an user by username, Returns empty result if user is not found. | |
91 | This command can be executed only using api_key belonging to user with admin |
|
91 | This command can be executed only using api_key belonging to user with admin | |
92 | rights. |
|
92 | rights. | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | INPUT:: |
|
95 | INPUT:: | |
96 |
|
96 | |||
97 | api_key : "<api_key>" |
|
97 | api_key : "<api_key>" | |
98 | method : "get_user" |
|
98 | method : "get_user" | |
99 | args : { |
|
99 | args : { | |
100 | "username" : "<username>" |
|
100 | "username" : "<username>" | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | OUTPUT:: |
|
103 | OUTPUT:: | |
104 |
|
104 | |||
105 | result: None if user does not exist or |
|
105 | result: None if user does not exist or | |
106 | { |
|
106 | { | |
107 | "id" : "<id>", |
|
107 | "id" : "<id>", | |
108 | "username" : "<username>", |
|
108 | "username" : "<username>", | |
109 | "firstname": "<firstname>", |
|
109 | "firstname": "<firstname>", | |
110 | "lastname" : "<lastname>", |
|
110 | "lastname" : "<lastname>", | |
111 | "email" : "<email>", |
|
111 | "email" : "<email>", | |
112 | "active" : "<bool>", |
|
112 | "active" : "<bool>", | |
113 | "admin" :Β "<bool>", |
|
113 | "admin" :Β "<bool>", | |
114 | "ldap" : "<ldap_dn>" |
|
114 | "ldap" : "<ldap_dn>" | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | error: null |
|
117 | error: null | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | get_users |
|
120 | get_users | |
121 | --------- |
|
121 | --------- | |
122 |
|
122 | |||
123 | Lists all existing users. This command can be executed only using api_key |
|
123 | Lists all existing users. This command can be executed only using api_key | |
124 | belonging to user with admin rights. |
|
124 | belonging to user with admin rights. | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | INPUT:: |
|
127 | INPUT:: | |
128 |
|
128 | |||
129 | api_key : "<api_key>" |
|
129 | api_key : "<api_key>" | |
130 | method : "get_users" |
|
130 | method : "get_users" | |
131 | args : { } |
|
131 | args : { } | |
132 |
|
132 | |||
133 | OUTPUT:: |
|
133 | OUTPUT:: | |
134 |
|
134 | |||
135 | result: [ |
|
135 | result: [ | |
136 | { |
|
136 | { | |
137 | "id" : "<id>", |
|
137 | "id" : "<id>", | |
138 | "username" : "<username>", |
|
138 | "username" : "<username>", | |
139 | "firstname": "<firstname>", |
|
139 | "firstname": "<firstname>", | |
140 | "lastname" : "<lastname>", |
|
140 | "lastname" : "<lastname>", | |
141 | "email" : "<email>", |
|
141 | "email" : "<email>", | |
142 | "active" : "<bool>", |
|
142 | "active" : "<bool>", | |
143 | "admin" :Β "<bool>", |
|
143 | "admin" :Β "<bool>", | |
144 | "ldap" : "<ldap_dn>" |
|
144 | "ldap" : "<ldap_dn>" | |
145 | }, |
|
145 | }, | |
146 | β¦ |
|
146 | β¦ | |
147 | ] |
|
147 | ] | |
148 | error: null |
|
148 | error: null | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | create_user |
|
151 | create_user | |
152 | ----------- |
|
152 | ----------- | |
153 |
|
153 | |||
154 |
Creates new user |
|
154 | Creates new user. This command can | |
155 | be executed only using api_key belonging to user with admin rights. |
|
155 | be executed only using api_key belonging to user with admin rights. | |
156 |
|
156 | |||
157 |
|
157 | |||
158 | INPUT:: |
|
158 | INPUT:: | |
159 |
|
159 | |||
160 | api_key : "<api_key>" |
|
160 | api_key : "<api_key>" | |
161 | method : "create_user" |
|
161 | method : "create_user" | |
162 | args : { |
|
162 | args : { | |
163 | "username" : "<username>", |
|
163 | "username" : "<username>", | |
164 | "password" : "<password>", |
|
164 | "password" : "<password>", | |
165 | "email" : "<useremail>", |
|
165 | "email" : "<useremail>", | |
166 | "firstname" : "<firstname> = None", |
|
166 | "firstname" : "<firstname> = None", | |
167 | "lastname" : "<lastname> = None", |
|
167 | "lastname" : "<lastname> = None", | |
168 | "active" : "<bool> = True", |
|
168 | "active" : "<bool> = True", | |
169 | "admin" : "<bool> = False", |
|
169 | "admin" : "<bool> = False", | |
170 | "ldap_dn" : "<ldap_dn> = None" |
|
170 | "ldap_dn" : "<ldap_dn> = None" | |
171 | } |
|
171 | } | |
172 |
|
172 | |||
173 | OUTPUT:: |
|
173 | OUTPUT:: | |
174 |
|
174 | |||
175 | result: { |
|
175 | result: { | |
176 | "id" : "<new_user_id>", |
|
176 | "id" : "<new_user_id>", | |
177 | "msg" : "created new user <username>" |
|
177 | "msg" : "created new user <username>" | |
178 | } |
|
178 | } | |
179 | error: null |
|
179 | error: null | |
180 |
|
180 | |||
181 |
|
181 | |||
|
182 | update_user | |||
|
183 | ----------- | |||
|
184 | ||||
|
185 | updates current one if such user exists. This command can | |||
|
186 | be executed only using api_key belonging to user with admin rights. | |||
|
187 | ||||
|
188 | ||||
|
189 | INPUT:: | |||
|
190 | ||||
|
191 | api_key : "<api_key>" | |||
|
192 | method : "update_user" | |||
|
193 | args : { | |||
|
194 | "username" : "<username>", | |||
|
195 | "password" : "<password>", | |||
|
196 | "email" : "<useremail>", | |||
|
197 | "firstname" : "<firstname> = None", | |||
|
198 | "lastname" : "<lastname> = None", | |||
|
199 | "active" : "<bool> = True", | |||
|
200 | "admin" : "<bool> = False", | |||
|
201 | "ldap_dn" : "<ldap_dn> = None" | |||
|
202 | } | |||
|
203 | ||||
|
204 | OUTPUT:: | |||
|
205 | ||||
|
206 | result: { | |||
|
207 | "id" : "<edited_user_id>", | |||
|
208 | "msg" : "updated user <username>" | |||
|
209 | } | |||
|
210 | error: null | |||
|
211 | ||||
|
212 | ||||
182 | get_users_group |
|
213 | get_users_group | |
183 | --------------- |
|
214 | --------------- | |
184 |
|
215 | |||
185 | Gets an existing users group. This command can be executed only using api_key |
|
216 | Gets an existing users group. This command can be executed only using api_key | |
186 | belonging to user with admin rights. |
|
217 | belonging to user with admin rights. | |
187 |
|
218 | |||
188 |
|
219 | |||
189 | INPUT:: |
|
220 | INPUT:: | |
190 |
|
221 | |||
191 | api_key : "<api_key>" |
|
222 | api_key : "<api_key>" | |
192 | method : "get_users_group" |
|
223 | method : "get_users_group" | |
193 | args : { |
|
224 | args : { | |
194 | "group_name" : "<name>" |
|
225 | "group_name" : "<name>" | |
195 | } |
|
226 | } | |
196 |
|
227 | |||
197 | OUTPUT:: |
|
228 | OUTPUT:: | |
198 |
|
229 | |||
199 | result : None if group not exist |
|
230 | result : None if group not exist | |
200 | { |
|
231 | { | |
201 | "id" : "<id>", |
|
232 | "id" : "<id>", | |
202 | "group_name" : "<groupname>", |
|
233 | "group_name" : "<groupname>", | |
203 | "active": "<bool>", |
|
234 | "active": "<bool>", | |
204 | "members" : [ |
|
235 | "members" : [ | |
205 | { "id" : "<userid>", |
|
236 | { "id" : "<userid>", | |
206 | "username" : "<username>", |
|
237 | "username" : "<username>", | |
207 | "firstname": "<firstname>", |
|
238 | "firstname": "<firstname>", | |
208 | "lastname" : "<lastname>", |
|
239 | "lastname" : "<lastname>", | |
209 | "email" : "<email>", |
|
240 | "email" : "<email>", | |
210 | "active" : "<bool>", |
|
241 | "active" : "<bool>", | |
211 | "admin" :Β "<bool>", |
|
242 | "admin" :Β "<bool>", | |
212 | "ldap" : "<ldap_dn>" |
|
243 | "ldap" : "<ldap_dn>" | |
213 | }, |
|
244 | }, | |
214 | β¦ |
|
245 | β¦ | |
215 | ] |
|
246 | ] | |
216 | } |
|
247 | } | |
217 | error : null |
|
248 | error : null | |
218 |
|
249 | |||
219 |
|
250 | |||
220 | get_users_groups |
|
251 | get_users_groups | |
221 | ---------------- |
|
252 | ---------------- | |
222 |
|
253 | |||
223 | Lists all existing users groups. This command can be executed only using |
|
254 | Lists all existing users groups. This command can be executed only using | |
224 | api_key belonging to user with admin rights. |
|
255 | api_key belonging to user with admin rights. | |
225 |
|
256 | |||
226 |
|
257 | |||
227 | INPUT:: |
|
258 | INPUT:: | |
228 |
|
259 | |||
229 | api_key : "<api_key>" |
|
260 | api_key : "<api_key>" | |
230 | method : "get_users_groups" |
|
261 | method : "get_users_groups" | |
231 | args : { } |
|
262 | args : { } | |
232 |
|
263 | |||
233 | OUTPUT:: |
|
264 | OUTPUT:: | |
234 |
|
265 | |||
235 | result : [ |
|
266 | result : [ | |
236 | { |
|
267 | { | |
237 | "id" : "<id>", |
|
268 | "id" : "<id>", | |
238 | "group_name" : "<groupname>", |
|
269 | "group_name" : "<groupname>", | |
239 | "active": "<bool>", |
|
270 | "active": "<bool>", | |
240 | "members" : [ |
|
271 | "members" : [ | |
241 | { |
|
272 | { | |
242 | "id" : "<userid>", |
|
273 | "id" : "<userid>", | |
243 | "username" : "<username>", |
|
274 | "username" : "<username>", | |
244 | "firstname": "<firstname>", |
|
275 | "firstname": "<firstname>", | |
245 | "lastname" : "<lastname>", |
|
276 | "lastname" : "<lastname>", | |
246 | "email" : "<email>", |
|
277 | "email" : "<email>", | |
247 | "active" : "<bool>", |
|
278 | "active" : "<bool>", | |
248 | "admin" :Β "<bool>", |
|
279 | "admin" :Β "<bool>", | |
249 | "ldap" : "<ldap_dn>" |
|
280 | "ldap" : "<ldap_dn>" | |
250 | }, |
|
281 | }, | |
251 | β¦ |
|
282 | β¦ | |
252 | ] |
|
283 | ] | |
253 | } |
|
284 | } | |
254 | ] |
|
285 | ] | |
255 | error : null |
|
286 | error : null | |
256 |
|
287 | |||
257 |
|
288 | |||
258 | create_users_group |
|
289 | create_users_group | |
259 | ------------------ |
|
290 | ------------------ | |
260 |
|
291 | |||
261 | Creates new users group. This command can be executed only using api_key |
|
292 | Creates new users group. This command can be executed only using api_key | |
262 | belonging to user with admin rights |
|
293 | belonging to user with admin rights | |
263 |
|
294 | |||
264 |
|
295 | |||
265 | INPUT:: |
|
296 | INPUT:: | |
266 |
|
297 | |||
267 | api_key : "<api_key>" |
|
298 | api_key : "<api_key>" | |
268 | method : "create_users_group" |
|
299 | method : "create_users_group" | |
269 | args: { |
|
300 | args: { | |
270 | "group_name": "<groupname>", |
|
301 | "group_name": "<groupname>", | |
271 | "active":"<bool> = True" |
|
302 | "active":"<bool> = True" | |
272 | } |
|
303 | } | |
273 |
|
304 | |||
274 | OUTPUT:: |
|
305 | OUTPUT:: | |
275 |
|
306 | |||
276 | result: { |
|
307 | result: { | |
277 | "id": "<newusersgroupid>", |
|
308 | "id": "<newusersgroupid>", | |
278 | "msg": "created new users group <groupname>" |
|
309 | "msg": "created new users group <groupname>" | |
279 | } |
|
310 | } | |
280 | error: null |
|
311 | error: null | |
281 |
|
312 | |||
282 |
|
313 | |||
283 | add_user_to_users_group |
|
314 | add_user_to_users_group | |
284 | ----------------------- |
|
315 | ----------------------- | |
285 |
|
316 | |||
286 | Adds a user to a users group. If user exists in that group success will be |
|
317 | Adds a user to a users group. If user exists in that group success will be | |
287 | `false`. This command can be executed only using api_key |
|
318 | `false`. This command can be executed only using api_key | |
288 | belonging to user with admin rights |
|
319 | belonging to user with admin rights | |
289 |
|
320 | |||
290 |
|
321 | |||
291 | INPUT:: |
|
322 | INPUT:: | |
292 |
|
323 | |||
293 | api_key : "<api_key>" |
|
324 | api_key : "<api_key>" | |
294 | method : "add_user_users_group" |
|
325 | method : "add_user_users_group" | |
295 | args: { |
|
326 | args: { | |
296 | "group_name" : "<groupname>", |
|
327 | "group_name" : "<groupname>", | |
297 | "username" : "<username>" |
|
328 | "username" : "<username>" | |
298 | } |
|
329 | } | |
299 |
|
330 | |||
300 | OUTPUT:: |
|
331 | OUTPUT:: | |
301 |
|
332 | |||
302 | result: { |
|
333 | result: { | |
303 | "id": "<newusersgroupmemberid>", |
|
334 | "id": "<newusersgroupmemberid>", | |
304 | "success": True|False # depends on if member is in group |
|
335 | "success": True|False # depends on if member is in group | |
305 | "msg": "added member <username> to users group <groupname> | |
|
336 | "msg": "added member <username> to users group <groupname> | | |
306 | User is already in that group" |
|
337 | User is already in that group" | |
307 | } |
|
338 | } | |
308 | error: null |
|
339 | error: null | |
309 |
|
340 | |||
310 |
|
341 | |||
311 | remove_user_from_users_group |
|
342 | remove_user_from_users_group | |
312 | ---------------------------- |
|
343 | ---------------------------- | |
313 |
|
344 | |||
314 | Removes a user from a users group. If user is not in given group success will |
|
345 | Removes a user from a users group. If user is not in given group success will | |
315 | be `false`. This command can be executed only |
|
346 | be `false`. This command can be executed only | |
316 | using api_key belonging to user with admin rights |
|
347 | using api_key belonging to user with admin rights | |
317 |
|
348 | |||
318 |
|
349 | |||
319 | INPUT:: |
|
350 | INPUT:: | |
320 |
|
351 | |||
321 | api_key : "<api_key>" |
|
352 | api_key : "<api_key>" | |
322 | method : "remove_user_from_users_group" |
|
353 | method : "remove_user_from_users_group" | |
323 | args: { |
|
354 | args: { | |
324 | "group_name" : "<groupname>", |
|
355 | "group_name" : "<groupname>", | |
325 | "username" : "<username>" |
|
356 | "username" : "<username>" | |
326 | } |
|
357 | } | |
327 |
|
358 | |||
328 | OUTPUT:: |
|
359 | OUTPUT:: | |
329 |
|
360 | |||
330 | result: { |
|
361 | result: { | |
331 | "success": True|False, # depends on if member is in group |
|
362 | "success": True|False, # depends on if member is in group | |
332 | "msg": "removed member <username> from users group <groupname> | |
|
363 | "msg": "removed member <username> from users group <groupname> | | |
333 | User wasn't in group" |
|
364 | User wasn't in group" | |
334 | } |
|
365 | } | |
335 | error: null |
|
366 | error: null | |
336 |
|
367 | |||
337 |
|
368 | |||
338 | get_repo |
|
369 | get_repo | |
339 | -------- |
|
370 | -------- | |
340 |
|
371 | |||
341 | Gets an existing repository. This command can be executed only using api_key |
|
372 | Gets an existing repository. This command can be executed only using api_key | |
342 | belonging to user with admin rights |
|
373 | belonging to user with admin rights | |
343 |
|
374 | |||
344 |
|
375 | |||
345 | INPUT:: |
|
376 | INPUT:: | |
346 |
|
377 | |||
347 | api_key : "<api_key>" |
|
378 | api_key : "<api_key>" | |
348 | method : "get_repo" |
|
379 | method : "get_repo" | |
349 | args: { |
|
380 | args: { | |
350 | "repo_name" : "<reponame>" |
|
381 | "repo_name" : "<reponame>" | |
351 | } |
|
382 | } | |
352 |
|
383 | |||
353 | OUTPUT:: |
|
384 | OUTPUT:: | |
354 |
|
385 | |||
355 | result: None if repository does not exist or |
|
386 | result: None if repository does not exist or | |
356 | { |
|
387 | { | |
357 | "id" : "<id>", |
|
388 | "id" : "<id>", | |
358 | "repo_name" : "<reponame>" |
|
389 | "repo_name" : "<reponame>" | |
359 | "type" : "<type>", |
|
390 | "type" : "<type>", | |
360 | "description" : "<description>", |
|
391 | "description" : "<description>", | |
361 | "members" : [ |
|
392 | "members" : [ | |
362 | { "id" : "<userid>", |
|
393 | { "id" : "<userid>", | |
363 | "username" : "<username>", |
|
394 | "username" : "<username>", | |
364 | "firstname": "<firstname>", |
|
395 | "firstname": "<firstname>", | |
365 | "lastname" : "<lastname>", |
|
396 | "lastname" : "<lastname>", | |
366 | "email" : "<email>", |
|
397 | "email" : "<email>", | |
367 | "active" : "<bool>", |
|
398 | "active" : "<bool>", | |
368 | "admin" :Β "<bool>", |
|
399 | "admin" :Β "<bool>", | |
369 | "ldap" : "<ldap_dn>", |
|
400 | "ldap" : "<ldap_dn>", | |
370 | "permission" : "repository.(read|write|admin)" |
|
401 | "permission" : "repository.(read|write|admin)" | |
371 | }, |
|
402 | }, | |
372 | β¦ |
|
403 | β¦ | |
373 | { |
|
404 | { | |
374 | "id" : "<usersgroupid>", |
|
405 | "id" : "<usersgroupid>", | |
375 | "name" : "<usersgroupname>", |
|
406 | "name" : "<usersgroupname>", | |
376 | "active": "<bool>", |
|
407 | "active": "<bool>", | |
377 | "permission" : "repository.(read|write|admin)" |
|
408 | "permission" : "repository.(read|write|admin)" | |
378 | }, |
|
409 | }, | |
379 | β¦ |
|
410 | β¦ | |
380 | ] |
|
411 | ] | |
381 | } |
|
412 | } | |
382 | error: null |
|
413 | error: null | |
383 |
|
414 | |||
384 |
|
415 | |||
385 | get_repos |
|
416 | get_repos | |
386 | --------- |
|
417 | --------- | |
387 |
|
418 | |||
388 | Lists all existing repositories. This command can be executed only using api_key |
|
419 | Lists all existing repositories. This command can be executed only using api_key | |
389 | belonging to user with admin rights |
|
420 | belonging to user with admin rights | |
390 |
|
421 | |||
391 |
|
422 | |||
392 | INPUT:: |
|
423 | INPUT:: | |
393 |
|
424 | |||
394 | api_key : "<api_key>" |
|
425 | api_key : "<api_key>" | |
395 | method : "get_repos" |
|
426 | method : "get_repos" | |
396 | args: { } |
|
427 | args: { } | |
397 |
|
428 | |||
398 | OUTPUT:: |
|
429 | OUTPUT:: | |
399 |
|
430 | |||
400 | result: [ |
|
431 | result: [ | |
401 | { |
|
432 | { | |
402 | "id" : "<id>", |
|
433 | "id" : "<id>", | |
403 | "repo_name" : "<reponame>" |
|
434 | "repo_name" : "<reponame>" | |
404 | "type" : "<type>", |
|
435 | "type" : "<type>", | |
405 | "description" : "<description>" |
|
436 | "description" : "<description>" | |
406 | }, |
|
437 | }, | |
407 | β¦ |
|
438 | β¦ | |
408 | ] |
|
439 | ] | |
409 | error: null |
|
440 | error: null | |
410 |
|
441 | |||
411 |
|
442 | |||
412 | get_repo_nodes |
|
443 | get_repo_nodes | |
413 | -------------- |
|
444 | -------------- | |
414 |
|
445 | |||
415 | returns a list of nodes and it's children in a flat list for a given path |
|
446 | returns a list of nodes and it's children in a flat list for a given path | |
416 | at given revision. It's possible to specify ret_type to show only `files` or |
|
447 | at given revision. It's possible to specify ret_type to show only `files` or | |
417 | `dirs`. This command can be executed only using api_key belonging to user |
|
448 | `dirs`. This command can be executed only using api_key belonging to user | |
418 | with admin rights |
|
449 | with admin rights | |
419 |
|
450 | |||
420 |
|
451 | |||
421 | INPUT:: |
|
452 | INPUT:: | |
422 |
|
453 | |||
423 | api_key : "<api_key>" |
|
454 | api_key : "<api_key>" | |
424 | method : "get_repo_nodes" |
|
455 | method : "get_repo_nodes" | |
425 | args: { |
|
456 | args: { | |
426 | "repo_name" : "<reponame>", |
|
457 | "repo_name" : "<reponame>", | |
427 | "revision" : "<revision>", |
|
458 | "revision" : "<revision>", | |
428 | "root_path" : "<root_path>", |
|
459 | "root_path" : "<root_path>", | |
429 | "ret_type" : "<ret_type>" = 'all' |
|
460 | "ret_type" : "<ret_type>" = 'all' | |
430 | } |
|
461 | } | |
431 |
|
462 | |||
432 | OUTPUT:: |
|
463 | OUTPUT:: | |
433 |
|
464 | |||
434 | result: [ |
|
465 | result: [ | |
435 | { |
|
466 | { | |
436 | "name" : "<name>" |
|
467 | "name" : "<name>" | |
437 | "type" : "<type>", |
|
468 | "type" : "<type>", | |
438 | }, |
|
469 | }, | |
439 | β¦ |
|
470 | β¦ | |
440 | ] |
|
471 | ] | |
441 | error: null |
|
472 | error: null | |
442 |
|
473 | |||
443 |
|
474 | |||
444 | create_repo |
|
475 | create_repo | |
445 | ----------- |
|
476 | ----------- | |
446 |
|
477 | |||
447 | Creates a repository. This command can be executed only using api_key |
|
478 | Creates a repository. This command can be executed only using api_key | |
448 | belonging to user with admin rights. |
|
479 | belonging to user with admin rights. | |
449 | If repository name contains "/", all needed repository groups will be created. |
|
480 | If repository name contains "/", all needed repository groups will be created. | |
450 | For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent), |
|
481 | For example "foo/bar/baz" will create groups "foo", "bar" (with "foo" as parent), | |
451 | and create "baz" repository with "bar" as group. |
|
482 | and create "baz" repository with "bar" as group. | |
452 |
|
483 | |||
453 |
|
484 | |||
454 | INPUT:: |
|
485 | INPUT:: | |
455 |
|
486 | |||
456 | api_key : "<api_key>" |
|
487 | api_key : "<api_key>" | |
457 | method : "create_repo" |
|
488 | method : "create_repo" | |
458 | args: { |
|
489 | args: { | |
459 | "repo_name" : "<reponame>", |
|
490 | "repo_name" : "<reponame>", | |
460 | "owner_name" : "<ownername>", |
|
491 | "owner_name" : "<ownername>", | |
461 | "description" : "<description> = ''", |
|
492 | "description" : "<description> = ''", | |
462 | "repo_type" : "<type> = 'hg'", |
|
493 | "repo_type" : "<type> = 'hg'", | |
463 | "private" : "<bool> = False" |
|
494 | "private" : "<bool> = False" | |
464 | } |
|
495 | } | |
465 |
|
496 | |||
466 | OUTPUT:: |
|
497 | OUTPUT:: | |
467 |
|
498 | |||
468 | result: { |
|
499 | result: { | |
469 | "id": "<newrepoid>", |
|
500 | "id": "<newrepoid>", | |
470 | "msg": "Created new repository <reponame>", |
|
501 | "msg": "Created new repository <reponame>", | |
471 | } |
|
502 | } | |
472 | error: null |
|
503 | error: null | |
473 |
|
504 | |||
474 |
|
505 | |||
475 | grant_user_permission |
|
506 | grant_user_permission | |
476 | --------------------- |
|
507 | --------------------- | |
477 |
|
508 | |||
478 | Grant permission for user on given repository, or update existing one |
|
509 | Grant permission for user on given repository, or update existing one | |
479 | if found. This command can be executed only using api_key belonging to user |
|
510 | if found. This command can be executed only using api_key belonging to user | |
480 | with admin rights. |
|
511 | with admin rights. | |
481 |
|
512 | |||
482 |
|
513 | |||
483 | INPUT:: |
|
514 | INPUT:: | |
484 |
|
515 | |||
485 | api_key : "<api_key>" |
|
516 | api_key : "<api_key>" | |
486 | method : "grant_user_permission" |
|
517 | method : "grant_user_permission" | |
487 | args: { |
|
518 | args: { | |
488 | "repo_name" : "<reponame>", |
|
519 | "repo_name" : "<reponame>", | |
489 | "username" : "<username>", |
|
520 | "username" : "<username>", | |
490 | "perm" : "(repository.(none|read|write|admin))", |
|
521 | "perm" : "(repository.(none|read|write|admin))", | |
491 | } |
|
522 | } | |
492 |
|
523 | |||
493 | OUTPUT:: |
|
524 | OUTPUT:: | |
494 |
|
525 | |||
495 | result: { |
|
526 | result: { | |
496 | "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>" |
|
527 | "msg" : "Granted perm: <perm> for user: <username> in repo: <reponame>" | |
497 | } |
|
528 | } | |
498 | error: null |
|
529 | error: null | |
499 |
|
530 | |||
500 |
|
531 | |||
501 | revoke_user_permission |
|
532 | revoke_user_permission | |
502 | ---------------------- |
|
533 | ---------------------- | |
503 |
|
534 | |||
504 | Revoke permission for user on given repository. This command can be executed |
|
535 | Revoke permission for user on given repository. This command can be executed | |
505 | only using api_key belonging to user with admin rights. |
|
536 | only using api_key belonging to user with admin rights. | |
506 |
|
537 | |||
507 |
|
538 | |||
508 | INPUT:: |
|
539 | INPUT:: | |
509 |
|
540 | |||
510 | api_key : "<api_key>" |
|
541 | api_key : "<api_key>" | |
511 | method : "revoke_user_permission" |
|
542 | method : "revoke_user_permission" | |
512 | args: { |
|
543 | args: { | |
513 | "repo_name" : "<reponame>", |
|
544 | "repo_name" : "<reponame>", | |
514 | "username" : "<username>", |
|
545 | "username" : "<username>", | |
515 | } |
|
546 | } | |
516 |
|
547 | |||
517 | OUTPUT:: |
|
548 | OUTPUT:: | |
518 |
|
549 | |||
519 | result: { |
|
550 | result: { | |
520 | "msg" : "Revoked perm for user: <suername> in repo: <reponame>" |
|
551 | "msg" : "Revoked perm for user: <suername> in repo: <reponame>" | |
521 | } |
|
552 | } | |
522 | error: null |
|
553 | error: null | |
523 |
|
554 | |||
524 |
|
555 | |||
525 | grant_users_group_permission |
|
556 | grant_users_group_permission | |
526 | ---------------------------- |
|
557 | ---------------------------- | |
527 |
|
558 | |||
528 | Grant permission for users group on given repository, or update |
|
559 | Grant permission for users group on given repository, or update | |
529 | existing one if found. This command can be executed only using |
|
560 | existing one if found. This command can be executed only using | |
530 | api_key belonging to user with admin rights. |
|
561 | api_key belonging to user with admin rights. | |
531 |
|
562 | |||
532 |
|
563 | |||
533 | INPUT:: |
|
564 | INPUT:: | |
534 |
|
565 | |||
535 | api_key : "<api_key>" |
|
566 | api_key : "<api_key>" | |
536 | method : "grant_users_group_permission" |
|
567 | method : "grant_users_group_permission" | |
537 | args: { |
|
568 | args: { | |
538 | "repo_name" : "<reponame>", |
|
569 | "repo_name" : "<reponame>", | |
539 | "group_name" : "<usersgroupname>", |
|
570 | "group_name" : "<usersgroupname>", | |
540 | "perm" : "(repository.(none|read|write|admin))", |
|
571 | "perm" : "(repository.(none|read|write|admin))", | |
541 | } |
|
572 | } | |
542 |
|
573 | |||
543 | OUTPUT:: |
|
574 | OUTPUT:: | |
544 |
|
575 | |||
545 | result: { |
|
576 | result: { | |
546 | "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>" |
|
577 | "msg" : "Granted perm: <perm> for group: <usersgroupname> in repo: <reponame>" | |
547 | } |
|
578 | } | |
548 | error: null |
|
579 | error: null | |
549 |
|
580 | |||
550 |
|
581 | |||
551 | revoke_users_group_permission |
|
582 | revoke_users_group_permission | |
552 | ----------------------------- |
|
583 | ----------------------------- | |
553 |
|
584 | |||
554 | Revoke permission for users group on given repository.This command can be |
|
585 | Revoke permission for users group on given repository.This command can be | |
555 | executed only using api_key belonging to user with admin rights. |
|
586 | executed only using api_key belonging to user with admin rights. | |
556 |
|
587 | |||
557 | INPUT:: |
|
588 | INPUT:: | |
558 |
|
589 | |||
559 | api_key : "<api_key>" |
|
590 | api_key : "<api_key>" | |
560 | method : "revoke_users_group_permission" |
|
591 | method : "revoke_users_group_permission" | |
561 | args: { |
|
592 | args: { | |
562 | "repo_name" : "<reponame>", |
|
593 | "repo_name" : "<reponame>", | |
563 | "users_group" : "<usersgroupname>", |
|
594 | "users_group" : "<usersgroupname>", | |
564 | } |
|
595 | } | |
565 |
|
596 | |||
566 | OUTPUT:: |
|
597 | OUTPUT:: | |
567 |
|
598 | |||
568 | result: { |
|
599 | result: { | |
569 | "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>" |
|
600 | "msg" : "Revoked perm for group: <usersgroupname> in repo: <reponame>" | |
570 | } |
|
601 | } | |
571 | error: null No newline at end of file |
|
602 | error: null |
@@ -1,604 +1,636 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.controllers.api |
|
3 | rhodecode.controllers.api | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | API controller for RhodeCode |
|
6 | API controller for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Aug 20, 2011 |
|
8 | :created_on: Aug 20, 2011 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 |
|
27 | |||
28 | import traceback |
|
28 | import traceback | |
29 | import logging |
|
29 | import logging | |
30 |
|
30 | |||
31 | from sqlalchemy.orm.exc import NoResultFound |
|
31 | from sqlalchemy.orm.exc import NoResultFound | |
32 |
|
32 | |||
33 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError |
|
33 | from rhodecode.controllers.api import JSONRPCController, JSONRPCError | |
34 | from rhodecode.lib.auth import HasPermissionAllDecorator, \ |
|
34 | from rhodecode.lib.auth import HasPermissionAllDecorator, \ | |
35 | HasPermissionAnyDecorator |
|
35 | HasPermissionAnyDecorator | |
36 |
|
36 | |||
37 | from rhodecode.model.meta import Session |
|
37 | from rhodecode.model.meta import Session | |
38 | from rhodecode.model.scm import ScmModel |
|
38 | from rhodecode.model.scm import ScmModel | |
39 | from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository |
|
39 | from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository | |
40 | from rhodecode.model.repo import RepoModel |
|
40 | from rhodecode.model.repo import RepoModel | |
41 | from rhodecode.model.user import UserModel |
|
41 | from rhodecode.model.user import UserModel | |
42 | from rhodecode.model.repo_permission import RepositoryPermissionModel |
|
42 | from rhodecode.model.repo_permission import RepositoryPermissionModel | |
43 | from rhodecode.model.users_group import UsersGroupModel |
|
43 | from rhodecode.model.users_group import UsersGroupModel | |
44 | from rhodecode.model.repos_group import ReposGroupModel |
|
44 | from rhodecode.model.repos_group import ReposGroupModel | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | log = logging.getLogger(__name__) |
|
47 | log = logging.getLogger(__name__) | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | class ApiController(JSONRPCController): |
|
50 | class ApiController(JSONRPCController): | |
51 | """ |
|
51 | """ | |
52 | API Controller |
|
52 | API Controller | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | Each method needs to have USER as argument this is then based on given |
|
55 | Each method needs to have USER as argument this is then based on given | |
56 | API_KEY propagated as instance of user object |
|
56 | API_KEY propagated as instance of user object | |
57 |
|
57 | |||
58 | Preferably this should be first argument also |
|
58 | Preferably this should be first argument also | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | Each function should also **raise** JSONRPCError for any |
|
61 | Each function should also **raise** JSONRPCError for any | |
62 | errors that happens |
|
62 | errors that happens | |
63 |
|
63 | |||
64 | """ |
|
64 | """ | |
65 |
|
65 | |||
66 | @HasPermissionAllDecorator('hg.admin') |
|
66 | @HasPermissionAllDecorator('hg.admin') | |
67 | def pull(self, apiuser, repo_name): |
|
67 | def pull(self, apiuser, repo_name): | |
68 | """ |
|
68 | """ | |
69 | Dispatch pull action on given repo |
|
69 | Dispatch pull action on given repo | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | :param user: |
|
72 | :param user: | |
73 | :param repo_name: |
|
73 | :param repo_name: | |
74 | """ |
|
74 | """ | |
75 |
|
75 | |||
76 | if Repository.is_valid(repo_name) is False: |
|
76 | if Repository.is_valid(repo_name) is False: | |
77 | raise JSONRPCError('Unknown repo "%s"' % repo_name) |
|
77 | raise JSONRPCError('Unknown repo "%s"' % repo_name) | |
78 |
|
78 | |||
79 | try: |
|
79 | try: | |
80 | ScmModel().pull_changes(repo_name, self.rhodecode_user.username) |
|
80 | ScmModel().pull_changes(repo_name, self.rhodecode_user.username) | |
81 | return 'Pulled from %s' % repo_name |
|
81 | return 'Pulled from %s' % repo_name | |
82 | except Exception: |
|
82 | except Exception: | |
83 | raise JSONRPCError('Unable to pull changes from "%s"' % repo_name) |
|
83 | raise JSONRPCError('Unable to pull changes from "%s"' % repo_name) | |
84 |
|
84 | |||
85 | @HasPermissionAllDecorator('hg.admin') |
|
85 | @HasPermissionAllDecorator('hg.admin') | |
86 | def get_user(self, apiuser, username): |
|
86 | def get_user(self, apiuser, username): | |
87 | """" |
|
87 | """" | |
88 | Get a user by username |
|
88 | Get a user by username | |
89 |
|
89 | |||
90 | :param apiuser: |
|
90 | :param apiuser: | |
91 | :param username: |
|
91 | :param username: | |
92 | """ |
|
92 | """ | |
93 |
|
93 | |||
94 | user = User.get_by_username(username) |
|
94 | user = User.get_by_username(username) | |
95 | if user is None: |
|
95 | if user is None: | |
96 | return user |
|
96 | return user | |
97 |
|
97 | |||
98 | return dict( |
|
98 | return dict( | |
99 | id=user.user_id, |
|
99 | id=user.user_id, | |
100 | username=user.username, |
|
100 | username=user.username, | |
101 | firstname=user.name, |
|
101 | firstname=user.name, | |
102 | lastname=user.lastname, |
|
102 | lastname=user.lastname, | |
103 | email=user.email, |
|
103 | email=user.email, | |
104 | active=user.active, |
|
104 | active=user.active, | |
105 | admin=user.admin, |
|
105 | admin=user.admin, | |
106 | ldap=user.ldap_dn |
|
106 | ldap=user.ldap_dn | |
107 | ) |
|
107 | ) | |
108 |
|
108 | |||
109 | @HasPermissionAllDecorator('hg.admin') |
|
109 | @HasPermissionAllDecorator('hg.admin') | |
110 | def get_users(self, apiuser): |
|
110 | def get_users(self, apiuser): | |
111 | """" |
|
111 | """" | |
112 | Get all users |
|
112 | Get all users | |
113 |
|
113 | |||
114 | :param apiuser: |
|
114 | :param apiuser: | |
115 | """ |
|
115 | """ | |
116 |
|
116 | |||
117 | result = [] |
|
117 | result = [] | |
118 | for user in User.getAll(): |
|
118 | for user in User.getAll(): | |
119 | result.append( |
|
119 | result.append( | |
120 | dict( |
|
120 | dict( | |
121 | id=user.user_id, |
|
121 | id=user.user_id, | |
122 | username=user.username, |
|
122 | username=user.username, | |
123 | firstname=user.name, |
|
123 | firstname=user.name, | |
124 | lastname=user.lastname, |
|
124 | lastname=user.lastname, | |
125 | email=user.email, |
|
125 | email=user.email, | |
126 | active=user.active, |
|
126 | active=user.active, | |
127 | admin=user.admin, |
|
127 | admin=user.admin, | |
128 | ldap=user.ldap_dn |
|
128 | ldap=user.ldap_dn | |
129 | ) |
|
129 | ) | |
130 | ) |
|
130 | ) | |
131 | return result |
|
131 | return result | |
132 |
|
132 | |||
133 | @HasPermissionAllDecorator('hg.admin') |
|
133 | @HasPermissionAllDecorator('hg.admin') | |
134 | def create_user(self, apiuser, username, password, email, firstname=None, |
|
134 | def create_user(self, apiuser, username, password, email, firstname=None, | |
135 | lastname=None, active=True, admin=False, ldap_dn=None): |
|
135 | lastname=None, active=True, admin=False, ldap_dn=None): | |
136 | """ |
|
136 | """ | |
137 |
Create new user |
|
137 | Create new user | |
138 |
|
138 | |||
139 | :param apiuser: |
|
139 | :param apiuser: | |
140 | :param username: |
|
140 | :param username: | |
141 | :param password: |
|
141 | :param password: | |
142 | :param email: |
|
142 | :param email: | |
143 | :param name: |
|
143 | :param name: | |
144 | :param lastname: |
|
144 | :param lastname: | |
145 | :param active: |
|
145 | :param active: | |
146 | :param admin: |
|
146 | :param admin: | |
147 | :param ldap_dn: |
|
147 | :param ldap_dn: | |
148 | """ |
|
148 | """ | |
149 |
|
||||
150 | if User.get_by_username(username): |
|
149 | if User.get_by_username(username): | |
151 | raise JSONRPCError("user %s already exist" % username) |
|
150 | raise JSONRPCError("user %s already exist" % username) | |
152 |
|
151 | |||
153 | try: |
|
152 | try: | |
154 | usr = UserModel().create_or_update( |
|
153 | usr = UserModel().create_or_update( | |
155 | username, password, email, firstname, |
|
154 | username, password, email, firstname, | |
156 | lastname, active, admin, ldap_dn |
|
155 | lastname, active, admin, ldap_dn | |
157 | ) |
|
156 | ) | |
158 | Session.commit() |
|
157 | Session.commit() | |
159 | return dict( |
|
158 | return dict( | |
160 | id=usr.user_id, |
|
159 | id=usr.user_id, | |
161 | msg='created new user %s' % username |
|
160 | msg='created new user %s' % username | |
162 | ) |
|
161 | ) | |
163 | except Exception: |
|
162 | except Exception: | |
164 | log.error(traceback.format_exc()) |
|
163 | log.error(traceback.format_exc()) | |
165 | raise JSONRPCError('failed to create user %s' % username) |
|
164 | raise JSONRPCError('failed to create user %s' % username) | |
166 |
|
165 | |||
167 | @HasPermissionAllDecorator('hg.admin') |
|
166 | @HasPermissionAllDecorator('hg.admin') | |
|
167 | def update_user(self, apiuser, username, password, email, firstname=None, | |||
|
168 | lastname=None, active=True, admin=False, ldap_dn=None): | |||
|
169 | """ | |||
|
170 | Updates given user | |||
|
171 | ||||
|
172 | :param apiuser: | |||
|
173 | :param username: | |||
|
174 | :param password: | |||
|
175 | :param email: | |||
|
176 | :param name: | |||
|
177 | :param lastname: | |||
|
178 | :param active: | |||
|
179 | :param admin: | |||
|
180 | :param ldap_dn: | |||
|
181 | """ | |||
|
182 | if not User.get_by_username(username): | |||
|
183 | raise JSONRPCError("user %s does not exist" % username) | |||
|
184 | ||||
|
185 | try: | |||
|
186 | usr = UserModel().create_or_update( | |||
|
187 | username, password, email, firstname, | |||
|
188 | lastname, active, admin, ldap_dn | |||
|
189 | ) | |||
|
190 | Session.commit() | |||
|
191 | return dict( | |||
|
192 | id=usr.user_id, | |||
|
193 | msg='updated user %s' % username | |||
|
194 | ) | |||
|
195 | except Exception: | |||
|
196 | log.error(traceback.format_exc()) | |||
|
197 | raise JSONRPCError('failed to update user %s' % username) | |||
|
198 | ||||
|
199 | @HasPermissionAllDecorator('hg.admin') | |||
168 | def get_users_group(self, apiuser, group_name): |
|
200 | def get_users_group(self, apiuser, group_name): | |
169 | """" |
|
201 | """" | |
170 | Get users group by name |
|
202 | Get users group by name | |
171 |
|
203 | |||
172 | :param apiuser: |
|
204 | :param apiuser: | |
173 | :param group_name: |
|
205 | :param group_name: | |
174 | """ |
|
206 | """ | |
175 |
|
207 | |||
176 | users_group = UsersGroup.get_by_group_name(group_name) |
|
208 | users_group = UsersGroup.get_by_group_name(group_name) | |
177 | if not users_group: |
|
209 | if not users_group: | |
178 | return None |
|
210 | return None | |
179 |
|
211 | |||
180 | members = [] |
|
212 | members = [] | |
181 | for user in users_group.members: |
|
213 | for user in users_group.members: | |
182 | user = user.user |
|
214 | user = user.user | |
183 | members.append(dict(id=user.user_id, |
|
215 | members.append(dict(id=user.user_id, | |
184 | username=user.username, |
|
216 | username=user.username, | |
185 | firstname=user.name, |
|
217 | firstname=user.name, | |
186 | lastname=user.lastname, |
|
218 | lastname=user.lastname, | |
187 | email=user.email, |
|
219 | email=user.email, | |
188 | active=user.active, |
|
220 | active=user.active, | |
189 | admin=user.admin, |
|
221 | admin=user.admin, | |
190 | ldap=user.ldap_dn)) |
|
222 | ldap=user.ldap_dn)) | |
191 |
|
223 | |||
192 | return dict(id=users_group.users_group_id, |
|
224 | return dict(id=users_group.users_group_id, | |
193 | group_name=users_group.users_group_name, |
|
225 | group_name=users_group.users_group_name, | |
194 | active=users_group.users_group_active, |
|
226 | active=users_group.users_group_active, | |
195 | members=members) |
|
227 | members=members) | |
196 |
|
228 | |||
197 | @HasPermissionAllDecorator('hg.admin') |
|
229 | @HasPermissionAllDecorator('hg.admin') | |
198 | def get_users_groups(self, apiuser): |
|
230 | def get_users_groups(self, apiuser): | |
199 | """" |
|
231 | """" | |
200 | Get all users groups |
|
232 | Get all users groups | |
201 |
|
233 | |||
202 | :param apiuser: |
|
234 | :param apiuser: | |
203 | """ |
|
235 | """ | |
204 |
|
236 | |||
205 | result = [] |
|
237 | result = [] | |
206 | for users_group in UsersGroup.getAll(): |
|
238 | for users_group in UsersGroup.getAll(): | |
207 | members = [] |
|
239 | members = [] | |
208 | for user in users_group.members: |
|
240 | for user in users_group.members: | |
209 | user = user.user |
|
241 | user = user.user | |
210 | members.append(dict(id=user.user_id, |
|
242 | members.append(dict(id=user.user_id, | |
211 | username=user.username, |
|
243 | username=user.username, | |
212 | firstname=user.name, |
|
244 | firstname=user.name, | |
213 | lastname=user.lastname, |
|
245 | lastname=user.lastname, | |
214 | email=user.email, |
|
246 | email=user.email, | |
215 | active=user.active, |
|
247 | active=user.active, | |
216 | admin=user.admin, |
|
248 | admin=user.admin, | |
217 | ldap=user.ldap_dn)) |
|
249 | ldap=user.ldap_dn)) | |
218 |
|
250 | |||
219 | result.append(dict(id=users_group.users_group_id, |
|
251 | result.append(dict(id=users_group.users_group_id, | |
220 | group_name=users_group.users_group_name, |
|
252 | group_name=users_group.users_group_name, | |
221 | active=users_group.users_group_active, |
|
253 | active=users_group.users_group_active, | |
222 | members=members)) |
|
254 | members=members)) | |
223 | return result |
|
255 | return result | |
224 |
|
256 | |||
225 | @HasPermissionAllDecorator('hg.admin') |
|
257 | @HasPermissionAllDecorator('hg.admin') | |
226 | def create_users_group(self, apiuser, group_name, active=True): |
|
258 | def create_users_group(self, apiuser, group_name, active=True): | |
227 | """ |
|
259 | """ | |
228 | Creates an new usergroup |
|
260 | Creates an new usergroup | |
229 |
|
261 | |||
230 | :param group_name: |
|
262 | :param group_name: | |
231 | :param active: |
|
263 | :param active: | |
232 | """ |
|
264 | """ | |
233 |
|
265 | |||
234 | if self.get_users_group(apiuser, group_name): |
|
266 | if self.get_users_group(apiuser, group_name): | |
235 | raise JSONRPCError("users group %s already exist" % group_name) |
|
267 | raise JSONRPCError("users group %s already exist" % group_name) | |
236 |
|
268 | |||
237 | try: |
|
269 | try: | |
238 | ug = UsersGroupModel().create(name=group_name, active=active) |
|
270 | ug = UsersGroupModel().create(name=group_name, active=active) | |
239 | Session.commit() |
|
271 | Session.commit() | |
240 | return dict(id=ug.users_group_id, |
|
272 | return dict(id=ug.users_group_id, | |
241 | msg='created new users group %s' % group_name) |
|
273 | msg='created new users group %s' % group_name) | |
242 | except Exception: |
|
274 | except Exception: | |
243 | log.error(traceback.format_exc()) |
|
275 | log.error(traceback.format_exc()) | |
244 | raise JSONRPCError('failed to create group %s' % group_name) |
|
276 | raise JSONRPCError('failed to create group %s' % group_name) | |
245 |
|
277 | |||
246 | @HasPermissionAllDecorator('hg.admin') |
|
278 | @HasPermissionAllDecorator('hg.admin') | |
247 | def add_user_to_users_group(self, apiuser, group_name, username): |
|
279 | def add_user_to_users_group(self, apiuser, group_name, username): | |
248 | """" |
|
280 | """" | |
249 | Add a user to a group |
|
281 | Add a user to a group | |
250 |
|
282 | |||
251 | :param apiuser: |
|
283 | :param apiuser: | |
252 | :param group_name: |
|
284 | :param group_name: | |
253 | :param username: |
|
285 | :param username: | |
254 | """ |
|
286 | """ | |
255 |
|
287 | |||
256 | try: |
|
288 | try: | |
257 | users_group = UsersGroup.get_by_group_name(group_name) |
|
289 | users_group = UsersGroup.get_by_group_name(group_name) | |
258 | if not users_group: |
|
290 | if not users_group: | |
259 | raise JSONRPCError('unknown users group %s' % group_name) |
|
291 | raise JSONRPCError('unknown users group %s' % group_name) | |
260 |
|
292 | |||
261 | user = User.get_by_username(username) |
|
293 | user = User.get_by_username(username) | |
262 | if user is None: |
|
294 | if user is None: | |
263 | raise JSONRPCError('unknown user %s' % username) |
|
295 | raise JSONRPCError('unknown user %s' % username) | |
264 |
|
296 | |||
265 | ugm = UsersGroupModel().add_user_to_group(users_group, user) |
|
297 | ugm = UsersGroupModel().add_user_to_group(users_group, user) | |
266 | success = True if ugm != True else False |
|
298 | success = True if ugm != True else False | |
267 | msg = 'added member %s to users group %s' % (username, group_name) |
|
299 | msg = 'added member %s to users group %s' % (username, group_name) | |
268 | msg = msg if success else 'User is already in that group' |
|
300 | msg = msg if success else 'User is already in that group' | |
269 | Session.commit() |
|
301 | Session.commit() | |
270 |
|
302 | |||
271 | return dict( |
|
303 | return dict( | |
272 | id=ugm.users_group_member_id if ugm != True else None, |
|
304 | id=ugm.users_group_member_id if ugm != True else None, | |
273 | success=success, |
|
305 | success=success, | |
274 | msg=msg |
|
306 | msg=msg | |
275 | ) |
|
307 | ) | |
276 | except Exception: |
|
308 | except Exception: | |
277 | log.error(traceback.format_exc()) |
|
309 | log.error(traceback.format_exc()) | |
278 | raise JSONRPCError('failed to add users group member') |
|
310 | raise JSONRPCError('failed to add users group member') | |
279 |
|
311 | |||
280 | @HasPermissionAllDecorator('hg.admin') |
|
312 | @HasPermissionAllDecorator('hg.admin') | |
281 | def remove_user_from_users_group(self, apiuser, group_name, username): |
|
313 | def remove_user_from_users_group(self, apiuser, group_name, username): | |
282 | """ |
|
314 | """ | |
283 | Remove user from a group |
|
315 | Remove user from a group | |
284 |
|
316 | |||
285 | :param apiuser |
|
317 | :param apiuser | |
286 | :param group_name |
|
318 | :param group_name | |
287 | :param username |
|
319 | :param username | |
288 | """ |
|
320 | """ | |
289 |
|
321 | |||
290 | try: |
|
322 | try: | |
291 | users_group = UsersGroup.get_by_group_name(group_name) |
|
323 | users_group = UsersGroup.get_by_group_name(group_name) | |
292 | if not users_group: |
|
324 | if not users_group: | |
293 | raise JSONRPCError('unknown users group %s' % group_name) |
|
325 | raise JSONRPCError('unknown users group %s' % group_name) | |
294 |
|
326 | |||
295 | user = User.get_by_username(username) |
|
327 | user = User.get_by_username(username) | |
296 | if user is None: |
|
328 | if user is None: | |
297 | raise JSONRPCError('unknown user %s' % username) |
|
329 | raise JSONRPCError('unknown user %s' % username) | |
298 |
|
330 | |||
299 | success = UsersGroupModel().remove_user_from_group(users_group, user) |
|
331 | success = UsersGroupModel().remove_user_from_group(users_group, user) | |
300 | msg = 'removed member %s from users group %s' % (username, group_name) |
|
332 | msg = 'removed member %s from users group %s' % (username, group_name) | |
301 | msg = msg if success else "User wasn't in group" |
|
333 | msg = msg if success else "User wasn't in group" | |
302 | Session.commit() |
|
334 | Session.commit() | |
303 | return dict(success=success, msg=msg) |
|
335 | return dict(success=success, msg=msg) | |
304 | except Exception: |
|
336 | except Exception: | |
305 | log.error(traceback.format_exc()) |
|
337 | log.error(traceback.format_exc()) | |
306 | raise JSONRPCError('failed to remove user from group') |
|
338 | raise JSONRPCError('failed to remove user from group') | |
307 |
|
339 | |||
308 | @HasPermissionAnyDecorator('hg.admin') |
|
340 | @HasPermissionAnyDecorator('hg.admin') | |
309 | def get_repo(self, apiuser, repo_name): |
|
341 | def get_repo(self, apiuser, repo_name): | |
310 | """" |
|
342 | """" | |
311 | Get repository by name |
|
343 | Get repository by name | |
312 |
|
344 | |||
313 | :param apiuser: |
|
345 | :param apiuser: | |
314 | :param repo_name: |
|
346 | :param repo_name: | |
315 | """ |
|
347 | """ | |
316 |
|
348 | |||
317 | repo = Repository.get_by_repo_name(repo_name) |
|
349 | repo = Repository.get_by_repo_name(repo_name) | |
318 | if repo is None: |
|
350 | if repo is None: | |
319 | raise JSONRPCError('unknown repository %s' % repo) |
|
351 | raise JSONRPCError('unknown repository %s' % repo) | |
320 |
|
352 | |||
321 | members = [] |
|
353 | members = [] | |
322 | for user in repo.repo_to_perm: |
|
354 | for user in repo.repo_to_perm: | |
323 | perm = user.permission.permission_name |
|
355 | perm = user.permission.permission_name | |
324 | user = user.user |
|
356 | user = user.user | |
325 | members.append( |
|
357 | members.append( | |
326 | dict( |
|
358 | dict( | |
327 | type_="user", |
|
359 | type_="user", | |
328 | id=user.user_id, |
|
360 | id=user.user_id, | |
329 | username=user.username, |
|
361 | username=user.username, | |
330 | firstname=user.name, |
|
362 | firstname=user.name, | |
331 | lastname=user.lastname, |
|
363 | lastname=user.lastname, | |
332 | email=user.email, |
|
364 | email=user.email, | |
333 | active=user.active, |
|
365 | active=user.active, | |
334 | admin=user.admin, |
|
366 | admin=user.admin, | |
335 | ldap=user.ldap_dn, |
|
367 | ldap=user.ldap_dn, | |
336 | permission=perm |
|
368 | permission=perm | |
337 | ) |
|
369 | ) | |
338 | ) |
|
370 | ) | |
339 | for users_group in repo.users_group_to_perm: |
|
371 | for users_group in repo.users_group_to_perm: | |
340 | perm = users_group.permission.permission_name |
|
372 | perm = users_group.permission.permission_name | |
341 | users_group = users_group.users_group |
|
373 | users_group = users_group.users_group | |
342 | members.append( |
|
374 | members.append( | |
343 | dict( |
|
375 | dict( | |
344 | type_="users_group", |
|
376 | type_="users_group", | |
345 | id=users_group.users_group_id, |
|
377 | id=users_group.users_group_id, | |
346 | name=users_group.users_group_name, |
|
378 | name=users_group.users_group_name, | |
347 | active=users_group.users_group_active, |
|
379 | active=users_group.users_group_active, | |
348 | permission=perm |
|
380 | permission=perm | |
349 | ) |
|
381 | ) | |
350 | ) |
|
382 | ) | |
351 |
|
383 | |||
352 | return dict( |
|
384 | return dict( | |
353 | id=repo.repo_id, |
|
385 | id=repo.repo_id, | |
354 | repo_name=repo.repo_name, |
|
386 | repo_name=repo.repo_name, | |
355 | type=repo.repo_type, |
|
387 | type=repo.repo_type, | |
356 | description=repo.description, |
|
388 | description=repo.description, | |
357 | members=members |
|
389 | members=members | |
358 | ) |
|
390 | ) | |
359 |
|
391 | |||
360 | @HasPermissionAnyDecorator('hg.admin') |
|
392 | @HasPermissionAnyDecorator('hg.admin') | |
361 | def get_repos(self, apiuser): |
|
393 | def get_repos(self, apiuser): | |
362 | """" |
|
394 | """" | |
363 | Get all repositories |
|
395 | Get all repositories | |
364 |
|
396 | |||
365 | :param apiuser: |
|
397 | :param apiuser: | |
366 | """ |
|
398 | """ | |
367 |
|
399 | |||
368 | result = [] |
|
400 | result = [] | |
369 | for repository in Repository.getAll(): |
|
401 | for repository in Repository.getAll(): | |
370 | result.append( |
|
402 | result.append( | |
371 | dict( |
|
403 | dict( | |
372 | id=repository.repo_id, |
|
404 | id=repository.repo_id, | |
373 | repo_name=repository.repo_name, |
|
405 | repo_name=repository.repo_name, | |
374 | type=repository.repo_type, |
|
406 | type=repository.repo_type, | |
375 | description=repository.description |
|
407 | description=repository.description | |
376 | ) |
|
408 | ) | |
377 | ) |
|
409 | ) | |
378 | return result |
|
410 | return result | |
379 |
|
411 | |||
380 | @HasPermissionAnyDecorator('hg.admin') |
|
412 | @HasPermissionAnyDecorator('hg.admin') | |
381 | def get_repo_nodes(self, apiuser, repo_name, revision, root_path, |
|
413 | def get_repo_nodes(self, apiuser, repo_name, revision, root_path, | |
382 | ret_type='all'): |
|
414 | ret_type='all'): | |
383 | """ |
|
415 | """ | |
384 | returns a list of nodes and it's children |
|
416 | returns a list of nodes and it's children | |
385 | for a given path at given revision. It's possible to specify ret_type |
|
417 | for a given path at given revision. It's possible to specify ret_type | |
386 | to show only files or dirs |
|
418 | to show only files or dirs | |
387 |
|
419 | |||
388 | :param apiuser: |
|
420 | :param apiuser: | |
389 | :param repo_name: name of repository |
|
421 | :param repo_name: name of repository | |
390 | :param revision: revision for which listing should be done |
|
422 | :param revision: revision for which listing should be done | |
391 | :param root_path: path from which start displaying |
|
423 | :param root_path: path from which start displaying | |
392 | :param ret_type: return type 'all|files|dirs' nodes |
|
424 | :param ret_type: return type 'all|files|dirs' nodes | |
393 | """ |
|
425 | """ | |
394 | try: |
|
426 | try: | |
395 | _d, _f = ScmModel().get_nodes(repo_name, revision, root_path, |
|
427 | _d, _f = ScmModel().get_nodes(repo_name, revision, root_path, | |
396 | flat=False) |
|
428 | flat=False) | |
397 | _map = { |
|
429 | _map = { | |
398 | 'all': _d + _f, |
|
430 | 'all': _d + _f, | |
399 | 'files': _f, |
|
431 | 'files': _f, | |
400 | 'dirs': _d, |
|
432 | 'dirs': _d, | |
401 | } |
|
433 | } | |
402 | return _map[ret_type] |
|
434 | return _map[ret_type] | |
403 | except KeyError: |
|
435 | except KeyError: | |
404 | raise JSONRPCError('ret_type must be one of %s' % _map.keys()) |
|
436 | raise JSONRPCError('ret_type must be one of %s' % _map.keys()) | |
405 | except Exception, e: |
|
437 | except Exception, e: | |
406 | raise JSONRPCError(e) |
|
438 | raise JSONRPCError(e) | |
407 |
|
439 | |||
408 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
440 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
409 | def create_repo(self, apiuser, repo_name, owner_name, description='', |
|
441 | def create_repo(self, apiuser, repo_name, owner_name, description='', | |
410 | repo_type='hg', private=False): |
|
442 | repo_type='hg', private=False): | |
411 | """ |
|
443 | """ | |
412 | Create a repository |
|
444 | Create a repository | |
413 |
|
445 | |||
414 | :param apiuser: |
|
446 | :param apiuser: | |
415 | :param repo_name: |
|
447 | :param repo_name: | |
416 | :param description: |
|
448 | :param description: | |
417 | :param type: |
|
449 | :param type: | |
418 | :param private: |
|
450 | :param private: | |
419 | :param owner_name: |
|
451 | :param owner_name: | |
420 | """ |
|
452 | """ | |
421 |
|
453 | |||
422 | try: |
|
454 | try: | |
423 | owner = User.get_by_username(owner_name) |
|
455 | owner = User.get_by_username(owner_name) | |
424 | if owner is None: |
|
456 | if owner is None: | |
425 | raise JSONRPCError('unknown user %s' % owner_name) |
|
457 | raise JSONRPCError('unknown user %s' % owner_name) | |
426 |
|
458 | |||
427 | if Repository.get_by_repo_name(repo_name): |
|
459 | if Repository.get_by_repo_name(repo_name): | |
428 | raise JSONRPCError("repo %s already exist" % repo_name) |
|
460 | raise JSONRPCError("repo %s already exist" % repo_name) | |
429 |
|
461 | |||
430 | groups = repo_name.split('/') |
|
462 | groups = repo_name.split('/') | |
431 | real_name = groups[-1] |
|
463 | real_name = groups[-1] | |
432 | groups = groups[:-1] |
|
464 | groups = groups[:-1] | |
433 | parent_id = None |
|
465 | parent_id = None | |
434 | for g in groups: |
|
466 | for g in groups: | |
435 | group = RepoGroup.get_by_group_name(g) |
|
467 | group = RepoGroup.get_by_group_name(g) | |
436 | if not group: |
|
468 | if not group: | |
437 | group = ReposGroupModel().create(g, '', parent_id) |
|
469 | group = ReposGroupModel().create(g, '', parent_id) | |
438 | parent_id = group.group_id |
|
470 | parent_id = group.group_id | |
439 |
|
471 | |||
440 | repo = RepoModel().create( |
|
472 | repo = RepoModel().create( | |
441 | dict( |
|
473 | dict( | |
442 | repo_name=real_name, |
|
474 | repo_name=real_name, | |
443 | repo_name_full=repo_name, |
|
475 | repo_name_full=repo_name, | |
444 | description=description, |
|
476 | description=description, | |
445 | private=private, |
|
477 | private=private, | |
446 | repo_type=repo_type, |
|
478 | repo_type=repo_type, | |
447 | repo_group=parent_id, |
|
479 | repo_group=parent_id, | |
448 | clone_uri=None |
|
480 | clone_uri=None | |
449 | ), |
|
481 | ), | |
450 | owner |
|
482 | owner | |
451 | ) |
|
483 | ) | |
452 | Session.commit() |
|
484 | Session.commit() | |
453 |
|
485 | |||
454 | return dict( |
|
486 | return dict( | |
455 | id=repo.repo_id, |
|
487 | id=repo.repo_id, | |
456 | msg="Created new repository %s" % repo.repo_name |
|
488 | msg="Created new repository %s" % repo.repo_name | |
457 | ) |
|
489 | ) | |
458 |
|
490 | |||
459 | except Exception: |
|
491 | except Exception: | |
460 | log.error(traceback.format_exc()) |
|
492 | log.error(traceback.format_exc()) | |
461 | raise JSONRPCError('failed to create repository %s' % repo_name) |
|
493 | raise JSONRPCError('failed to create repository %s' % repo_name) | |
462 |
|
494 | |||
463 | @HasPermissionAnyDecorator('hg.admin') |
|
495 | @HasPermissionAnyDecorator('hg.admin') | |
464 | def grant_user_permission(self, repo_name, username, perm): |
|
496 | def grant_user_permission(self, repo_name, username, perm): | |
465 | """ |
|
497 | """ | |
466 | Grant permission for user on given repository, or update existing one |
|
498 | Grant permission for user on given repository, or update existing one | |
467 | if found |
|
499 | if found | |
468 |
|
500 | |||
469 | :param repo_name: |
|
501 | :param repo_name: | |
470 | :param username: |
|
502 | :param username: | |
471 | :param perm: |
|
503 | :param perm: | |
472 | """ |
|
504 | """ | |
473 |
|
505 | |||
474 | try: |
|
506 | try: | |
475 | repo = Repository.get_by_repo_name(repo_name) |
|
507 | repo = Repository.get_by_repo_name(repo_name) | |
476 | if repo is None: |
|
508 | if repo is None: | |
477 | raise JSONRPCError('unknown repository %s' % repo) |
|
509 | raise JSONRPCError('unknown repository %s' % repo) | |
478 |
|
510 | |||
479 | user = User.get_by_username(username) |
|
511 | user = User.get_by_username(username) | |
480 | if user is None: |
|
512 | if user is None: | |
481 | raise JSONRPCError('unknown user %s' % username) |
|
513 | raise JSONRPCError('unknown user %s' % username) | |
482 |
|
514 | |||
483 | RepoModel().grant_user_permission(repo=repo, user=user, perm=perm) |
|
515 | RepoModel().grant_user_permission(repo=repo, user=user, perm=perm) | |
484 |
|
516 | |||
485 | Session.commit() |
|
517 | Session.commit() | |
486 | return dict( |
|
518 | return dict( | |
487 | msg='Granted perm: %s for user: %s in repo: %s' % ( |
|
519 | msg='Granted perm: %s for user: %s in repo: %s' % ( | |
488 | perm, username, repo_name |
|
520 | perm, username, repo_name | |
489 | ) |
|
521 | ) | |
490 | ) |
|
522 | ) | |
491 | except Exception: |
|
523 | except Exception: | |
492 | log.error(traceback.format_exc()) |
|
524 | log.error(traceback.format_exc()) | |
493 | raise JSONRPCError( |
|
525 | raise JSONRPCError( | |
494 | 'failed to edit permission %(repo)s for %(user)s' % dict( |
|
526 | 'failed to edit permission %(repo)s for %(user)s' % dict( | |
495 | user=username, repo=repo_name |
|
527 | user=username, repo=repo_name | |
496 | ) |
|
528 | ) | |
497 | ) |
|
529 | ) | |
498 |
|
530 | |||
499 | @HasPermissionAnyDecorator('hg.admin') |
|
531 | @HasPermissionAnyDecorator('hg.admin') | |
500 | def revoke_user_permission(self, repo_name, username): |
|
532 | def revoke_user_permission(self, repo_name, username): | |
501 | """ |
|
533 | """ | |
502 | Revoke permission for user on given repository |
|
534 | Revoke permission for user on given repository | |
503 |
|
535 | |||
504 | :param repo_name: |
|
536 | :param repo_name: | |
505 | :param username: |
|
537 | :param username: | |
506 | """ |
|
538 | """ | |
507 |
|
539 | |||
508 | try: |
|
540 | try: | |
509 | repo = Repository.get_by_repo_name(repo_name) |
|
541 | repo = Repository.get_by_repo_name(repo_name) | |
510 | if repo is None: |
|
542 | if repo is None: | |
511 | raise JSONRPCError('unknown repository %s' % repo) |
|
543 | raise JSONRPCError('unknown repository %s' % repo) | |
512 |
|
544 | |||
513 | user = User.get_by_username(username) |
|
545 | user = User.get_by_username(username) | |
514 | if user is None: |
|
546 | if user is None: | |
515 | raise JSONRPCError('unknown user %s' % username) |
|
547 | raise JSONRPCError('unknown user %s' % username) | |
516 |
|
548 | |||
517 | RepoModel().revoke_user_permission(repo=repo_name, user=username) |
|
549 | RepoModel().revoke_user_permission(repo=repo_name, user=username) | |
518 |
|
550 | |||
519 | Session.commit() |
|
551 | Session.commit() | |
520 | return dict( |
|
552 | return dict( | |
521 | msg='Revoked perm for user: %s in repo: %s' % ( |
|
553 | msg='Revoked perm for user: %s in repo: %s' % ( | |
522 | username, repo_name |
|
554 | username, repo_name | |
523 | ) |
|
555 | ) | |
524 | ) |
|
556 | ) | |
525 | except Exception: |
|
557 | except Exception: | |
526 | log.error(traceback.format_exc()) |
|
558 | log.error(traceback.format_exc()) | |
527 | raise JSONRPCError( |
|
559 | raise JSONRPCError( | |
528 | 'failed to edit permission %(repo)s for %(user)s' % dict( |
|
560 | 'failed to edit permission %(repo)s for %(user)s' % dict( | |
529 | user=username, repo=repo_name |
|
561 | user=username, repo=repo_name | |
530 | ) |
|
562 | ) | |
531 | ) |
|
563 | ) | |
532 |
|
564 | |||
533 | @HasPermissionAnyDecorator('hg.admin') |
|
565 | @HasPermissionAnyDecorator('hg.admin') | |
534 | def grant_users_group_permission(self, repo_name, group_name, perm): |
|
566 | def grant_users_group_permission(self, repo_name, group_name, perm): | |
535 | """ |
|
567 | """ | |
536 | Grant permission for users group on given repository, or update |
|
568 | Grant permission for users group on given repository, or update | |
537 | existing one if found |
|
569 | existing one if found | |
538 |
|
570 | |||
539 | :param repo_name: |
|
571 | :param repo_name: | |
540 | :param group_name: |
|
572 | :param group_name: | |
541 | :param perm: |
|
573 | :param perm: | |
542 | """ |
|
574 | """ | |
543 |
|
575 | |||
544 | try: |
|
576 | try: | |
545 | repo = Repository.get_by_repo_name(repo_name) |
|
577 | repo = Repository.get_by_repo_name(repo_name) | |
546 | if repo is None: |
|
578 | if repo is None: | |
547 | raise JSONRPCError('unknown repository %s' % repo) |
|
579 | raise JSONRPCError('unknown repository %s' % repo) | |
548 |
|
580 | |||
549 | user_group = UsersGroup.get_by_group_name(group_name) |
|
581 | user_group = UsersGroup.get_by_group_name(group_name) | |
550 | if user_group is None: |
|
582 | if user_group is None: | |
551 | raise JSONRPCError('unknown users group %s' % user_group) |
|
583 | raise JSONRPCError('unknown users group %s' % user_group) | |
552 |
|
584 | |||
553 | RepoModel().grant_users_group_permission(repo=repo_name, |
|
585 | RepoModel().grant_users_group_permission(repo=repo_name, | |
554 | group_name=group_name, |
|
586 | group_name=group_name, | |
555 | perm=perm) |
|
587 | perm=perm) | |
556 |
|
588 | |||
557 | Session.commit() |
|
589 | Session.commit() | |
558 | return dict( |
|
590 | return dict( | |
559 | msg='Granted perm: %s for group: %s in repo: %s' % ( |
|
591 | msg='Granted perm: %s for group: %s in repo: %s' % ( | |
560 | perm, group_name, repo_name |
|
592 | perm, group_name, repo_name | |
561 | ) |
|
593 | ) | |
562 | ) |
|
594 | ) | |
563 | except Exception: |
|
595 | except Exception: | |
564 | log.error(traceback.format_exc()) |
|
596 | log.error(traceback.format_exc()) | |
565 | raise JSONRPCError( |
|
597 | raise JSONRPCError( | |
566 | 'failed to edit permission %(repo)s for %(usersgr)s' % dict( |
|
598 | 'failed to edit permission %(repo)s for %(usersgr)s' % dict( | |
567 | usersgr=group_name, repo=repo_name |
|
599 | usersgr=group_name, repo=repo_name | |
568 | ) |
|
600 | ) | |
569 | ) |
|
601 | ) | |
570 |
|
602 | |||
571 | @HasPermissionAnyDecorator('hg.admin') |
|
603 | @HasPermissionAnyDecorator('hg.admin') | |
572 | def revoke_users_group_permission(self, repo_name, group_name): |
|
604 | def revoke_users_group_permission(self, repo_name, group_name): | |
573 | """ |
|
605 | """ | |
574 | Revoke permission for users group on given repository |
|
606 | Revoke permission for users group on given repository | |
575 |
|
607 | |||
576 | :param repo_name: |
|
608 | :param repo_name: | |
577 | :param group_name: |
|
609 | :param group_name: | |
578 | """ |
|
610 | """ | |
579 |
|
611 | |||
580 | try: |
|
612 | try: | |
581 | repo = Repository.get_by_repo_name(repo_name) |
|
613 | repo = Repository.get_by_repo_name(repo_name) | |
582 | if repo is None: |
|
614 | if repo is None: | |
583 | raise JSONRPCError('unknown repository %s' % repo) |
|
615 | raise JSONRPCError('unknown repository %s' % repo) | |
584 |
|
616 | |||
585 | user_group = UsersGroup.get_by_group_name(group_name) |
|
617 | user_group = UsersGroup.get_by_group_name(group_name) | |
586 | if user_group is None: |
|
618 | if user_group is None: | |
587 | raise JSONRPCError('unknown users group %s' % user_group) |
|
619 | raise JSONRPCError('unknown users group %s' % user_group) | |
588 |
|
620 | |||
589 | RepoModel().revoke_users_group_permission(repo=repo_name, |
|
621 | RepoModel().revoke_users_group_permission(repo=repo_name, | |
590 | group_name=group_name) |
|
622 | group_name=group_name) | |
591 |
|
623 | |||
592 | Session.commit() |
|
624 | Session.commit() | |
593 | return dict( |
|
625 | return dict( | |
594 | msg='Revoked perm for group: %s in repo: %s' % ( |
|
626 | msg='Revoked perm for group: %s in repo: %s' % ( | |
595 | group_name, repo_name |
|
627 | group_name, repo_name | |
596 | ) |
|
628 | ) | |
597 | ) |
|
629 | ) | |
598 | except Exception: |
|
630 | except Exception: | |
599 | log.error(traceback.format_exc()) |
|
631 | log.error(traceback.format_exc()) | |
600 | raise JSONRPCError( |
|
632 | raise JSONRPCError( | |
601 | 'failed to edit permission %(repo)s for %(usersgr)s' % dict( |
|
633 | 'failed to edit permission %(repo)s for %(usersgr)s' % dict( | |
602 | usersgr=group_name, repo=repo_name |
|
634 | usersgr=group_name, repo=repo_name | |
603 | ) |
|
635 | ) | |
604 | ) |
|
636 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now