Commit 339555846e2ddc6606bddeb2590a92f5d882e33c
Exists in
master
and in
4 other branches
Merge branch 'api_for_user_creation' of dev.gitlabhq.com:gitlabhq
Showing
4 changed files
with
72 additions
and
0 deletions
Show diff stats
doc/api/users.md
| ... | ... | @@ -65,6 +65,27 @@ Parameters: |
| 65 | 65 | } |
| 66 | 66 | ``` |
| 67 | 67 | |
| 68 | +## User creation | |
| 69 | +Create user. Available only for admin | |
| 70 | + | |
| 71 | +``` | |
| 72 | +POST /users | |
| 73 | +``` | |
| 74 | + | |
| 75 | +Parameters: | |
| 76 | ++ `email` (required) - Email | |
| 77 | ++ `name` (required) - Name | |
| 78 | ++ `password` (required) - Password | |
| 79 | ++ `password_confirmation` (required) - Password confirmation | |
| 80 | ++ `skype` - Skype ID | |
| 81 | ++ `linkedin` (required) - Linkedin | |
| 82 | ++ `twitter` - Twitter account | |
| 83 | ++ `projects_limit` - Limit projects wich user can create | |
| 84 | + | |
| 85 | + | |
| 86 | +Will return created user with status `201 Created` on success, or `404 Not | |
| 87 | +found` on fail. | |
| 88 | + | |
| 68 | 89 | ## Current user |
| 69 | 90 | |
| 70 | 91 | Get currently authenticated user. | ... | ... |
lib/api/helpers.rb
| ... | ... | @@ -22,6 +22,10 @@ module Gitlab |
| 22 | 22 | unauthorized! unless current_user |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | + def authenticated_as_admin! | |
| 26 | + forbidden! unless current_user.is_admin? | |
| 27 | + end | |
| 28 | + | |
| 25 | 29 | def authorize! action, subject |
| 26 | 30 | unless abilities.allowed?(current_user, action, subject) |
| 27 | 31 | forbidden! | ... | ... |
lib/api/users.rb
| ... | ... | @@ -23,6 +23,30 @@ module Gitlab |
| 23 | 23 | @user = User.find(params[:id]) |
| 24 | 24 | present @user, with: Entities::User |
| 25 | 25 | end |
| 26 | + | |
| 27 | + # Create user. Available only for admin | |
| 28 | + # | |
| 29 | + # Parameters: | |
| 30 | + # email (required) - Email | |
| 31 | + # name (required) - Name | |
| 32 | + # password (required) - Password | |
| 33 | + # password_confirmation (required) - Password confirmation | |
| 34 | + # skype - Skype ID | |
| 35 | + # linkedin (required) - Linkedin | |
| 36 | + # twitter - Twitter account | |
| 37 | + # projects_limit - Limit projects wich user can create | |
| 38 | + # Example Request: | |
| 39 | + # POST /users | |
| 40 | + post do | |
| 41 | + authenticated_as_admin! | |
| 42 | + attrs = attributes_for_keys [:email, :name, :password, :password_confirmation, :skype, :linkedin, :twitter, :projects_limit] | |
| 43 | + user = User.new attrs | |
| 44 | + if user.save | |
| 45 | + present user, with: Entities::User | |
| 46 | + else | |
| 47 | + not_found! | |
| 48 | + end | |
| 49 | + end | |
| 26 | 50 | end |
| 27 | 51 | |
| 28 | 52 | resource :user do |
| ... | ... | @@ -78,6 +102,8 @@ module Gitlab |
| 78 | 102 | key = current_user.keys.find params[:id] |
| 79 | 103 | key.delete |
| 80 | 104 | end |
| 105 | + | |
| 106 | + | |
| 81 | 107 | end |
| 82 | 108 | end |
| 83 | 109 | end | ... | ... |
spec/requests/api/users_spec.rb
| ... | ... | @@ -4,6 +4,7 @@ describe Gitlab::API do |
| 4 | 4 | include ApiHelpers |
| 5 | 5 | |
| 6 | 6 | let(:user) { Factory :user } |
| 7 | + let(:admin) {Factory :admin} | |
| 7 | 8 | let(:key) { Factory :key, user: user } |
| 8 | 9 | |
| 9 | 10 | describe "GET /users" do |
| ... | ... | @@ -32,6 +33,26 @@ describe Gitlab::API do |
| 32 | 33 | end |
| 33 | 34 | end |
| 34 | 35 | |
| 36 | + describe "POST /users" do | |
| 37 | + before{ admin } | |
| 38 | + | |
| 39 | + it "should not create invalid user" do | |
| 40 | + post api("/users", admin), { email: "invalid email" } | |
| 41 | + response.status.should == 404 | |
| 42 | + end | |
| 43 | + | |
| 44 | + it "should create user" do | |
| 45 | + expect{ | |
| 46 | + post api("/users", admin), Factory.attributes(:user) | |
| 47 | + }.to change{User.count}.by(1) | |
| 48 | + end | |
| 49 | + | |
| 50 | + it "shouldn't available for non admin users" do | |
| 51 | + post api("/users", user), Factory.attributes(:user) | |
| 52 | + response.status.should == 403 | |
| 53 | + end | |
| 54 | + end | |
| 55 | + | |
| 35 | 56 | describe "GET /user" do |
| 36 | 57 | it "should return current user" do |
| 37 | 58 | get api("/user", user) | ... | ... |