Skip to content

Add api endpoint for setting user password to be handled by moocfi #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/controllers/api/v8/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ class UsersController < Api::V8::BaseController
end
end

swagger_path '/api/v8/users/{user_id}/set_password_managed_by_moocfi' do
operation :post do
key :description, 'Sets the boolean password_managed_by_moocfi for the user with the given id to payload value.'
key :operationId, 'setPasswordManagedByMoocfi'
key :produces, ['application/json']
key :tags, ['user']
parameter '$ref': '#/parameters/user_id'
response 403, '$ref': '#/responses/error'
response 404, '$ref': '#/responses/error'
response 200 do
key :description, "status 'ok' and sets the boolean password_managed_by_moocfi"
schema do
key :title, :status
key :required, [:status]
property :status, type: :string, example: 'Password managed by Mooc.fi set to true.'
end
end
end
end


def show
unauthorize_guest! if current_user.guest?
user = current_user
Expand Down Expand Up @@ -149,6 +170,28 @@ def update
}, status: :bad_request
end

def set_password_managed_by_moocfi
unauthorize_guest! if current_user.guest?

@user = User.find_by!(id: params[:id])
authorize! :update, @user

value = params[:set_password_managed_by_moocfi]
unless value.in?([true, false])
@user.errors.add(:password_managed_by_moocfi, 'must be a boolean')
else
@user.password_managed_by_moocfi = value
end

if @user.errors.any? || [email protected]
render json: { errors: @user.errors }, status: :bad_request
else
render json: {
status: "Password managed by Mooc.fi set to #{value}."
}
end
end

private
def set_email
user_params = params[:user]
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
resources :request_deletion, only: [:create], module: :users
resources :assistantships, module: :users, only: :index
resources :teacherships, module: :users, only: :index
post :set_password_managed_by_moocfi, on: :member
end

resources :user_app_datum, only: [:index]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPasswordManagedByMoocfiToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :password_managed_by_moocfi, :boolean, default: false
end
end
Loading
Loading