Skip to content

Commit b25a155

Browse files
committed
Add api endpoint for setting user password to be handled by moocfi
1 parent f0cc000 commit b25a155

File tree

4 files changed

+132
-88
lines changed

4 files changed

+132
-88
lines changed

app/controllers/api/v8/users_controller.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ class UsersController < Api::V8::BaseController
5656
end
5757
end
5858

59+
swagger_path '/api/v8/users/{user_id}/set_password_managed_by_moocfi' do
60+
operation :post do
61+
key :description, 'Sets the boolean password_managed_by_moocfi for the user with the given id to payload value.'
62+
key :operationId, 'setPasswordManagedByMoocfi'
63+
key :produces, ['application/json']
64+
key :tags, ['user']
65+
parameter '$ref': '#/parameters/user_id'
66+
response 403, '$ref': '#/responses/error'
67+
response 404, '$ref': '#/responses/error'
68+
response 200 do
69+
key :description, "status 'ok' and sets the boolean password_managed_by_moocfi"
70+
schema do
71+
key :title, :status
72+
key :required, [:status]
73+
property :status, type: :string, example: 'Password managed by Mooc.fi set to true.'
74+
end
75+
end
76+
end
77+
end
78+
79+
5980
def show
6081
unauthorize_guest! if current_user.guest?
6182
user = current_user
@@ -149,6 +170,23 @@ def update
149170
}, status: :bad_request
150171
end
151172

173+
def set_password_managed_by_moocfi
174+
unauthorize_guest! if current_user.guest?
175+
176+
@user = User.find_by!(id: params[:id])
177+
authorize! :update, @user
178+
@user.password_managed_by_moocfi = params[:set_password_managed_by_moocfi]
179+
if @user.save
180+
render json: {
181+
status: "Password managed by Mooc.fi set to #{params[:set_password_managed_by_moocfi]}."
182+
}
183+
else
184+
render json: {
185+
errors: @user.errors
186+
}, status: :bad_request
187+
end
188+
end
189+
152190
private
153191
def set_email
154192
user_params = params[:user]

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
resources :request_deletion, only: [:create], module: :users
6060
resources :assistantships, module: :users, only: :index
6161
resources :teacherships, module: :users, only: :index
62+
post :set_password_managed_by_moocfi, on: :member
6263
end
6364

6465
resources :user_app_datum, only: [:index]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPasswordManagedByMoocfiToUsers < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :users, :password_managed_by_moocfi, :boolean, default: false
4+
end
5+
end

0 commit comments

Comments
 (0)