diff options
author | Patricio Cano <suprnova32@gmail.com> | 2015-09-22 16:26:59 -0500 |
---|---|---|
committer | Patricio Cano <suprnova32@gmail.com> | 2015-09-22 16:26:59 -0500 |
commit | f60eb60473a1dcfd2b874d5ebac6dca60da7c1ea (patch) | |
tree | 863c43ad056482e10c2e3ece4f5aecffd9cc424e | |
parent | e0a23791d3cdf7a72e974acc79c3755b35f2da79 (diff) | |
download | gitlab-ce-f60eb60473a1dcfd2b874d5ebac6dca60da7c1ea.tar.gz |
Added ability to update or set the identity of an existing user, like the documentation said it was possible, but actually wasn't.fix-user-identities-api
-rw-r--r-- | lib/api/users.rb | 11 | ||||
-rw-r--r-- | spec/requests/api/users_spec.rb | 14 |
2 files changed, 25 insertions, 0 deletions
diff --git a/lib/api/users.rb b/lib/api/users.rb index 813cc379e43..a98d668e02d 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -121,6 +121,17 @@ module API User.where(username: attrs[:username]). where.not(id: user.id).count > 0 + identity_attrs = attributes_for_keys [:provider, :extern_uid] + if identity_attrs.any? + identity = user.identities.find_by(provider: identity_attrs[:provider]) + if identity + identity.update_attributes(identity_attrs) + else + identity = user.identities.build(identity_attrs) + identity.save + end + end + if user.update_attributes(attrs) present user, with: Entities::UserFull else diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index f9bc63680ba..d26a300ed82 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -7,6 +7,7 @@ describe API::API, api: true do let(:admin) { create(:admin) } let(:key) { create(:key, user: user) } let(:email) { create(:email, user: user) } + let(:omniauth_user) { create(:omniauth_user) } describe "GET /users" do context "when unauthenticated" do @@ -230,6 +231,19 @@ describe API::API, api: true do expect(user.reload.username).to eq(user.username) end + it "should update user's existing identity" do + put api("/users/#{omniauth_user.id}", admin), provider: 'ldapmain', extern_uid: '654321' + expect(response.status).to eq(200) + expect(omniauth_user.reload.identities.first.extern_uid).to eq('654321') + end + + it 'should update user with new identity' do + put api("/users/#{user.id}", admin), provider: 'github', extern_uid: '67890' + expect(response.status).to eq(200) + expect(user.reload.identities.first.extern_uid).to eq('67890') + expect(user.reload.identities.first.provider).to eq('github') + end + it "should update admin status" do put api("/users/#{user.id}", admin), { admin: true } expect(response.status).to eq(200) |