Skip to content

Instantly share code, notes, and snippets.

@ltello
Last active May 6, 2020 16:35
Show Gist options
  • Save ltello/d993a6908111fc5fe54e65986435c2c4 to your computer and use it in GitHub Desktop.
Save ltello/d993a6908111fc5fe54e65986435c2c4 to your computer and use it in GitHub Desktop.
A sample controller to manage Identities linking Users to Kong Consumers
# frozen_string_literal: true
# @tag Identities
class IdentitiesController < ApplicationController
attr_reader :identity, :installer, :password, :token
before_action :create_identity, only: %i[create]
before_action :set_token, only: %i[change_password validate_reset_password_token]
before_action :set_identity, only: %i[destroy show update]
before_action :set_installer_identity, only: %i[change_password validate_reset_password_token password_resetable]
before_action :set_installer, only: %i[password_resetable]
before_action :check_installer, only: %i[password_resetable]
before_action :check_token_expiry, only: %i[change_password validate_reset_password_token]
before_action :check_token, only: %i[change_password validate_reset_password_token]
before_action :set_password, only: %i[change_password]
# Show an identity
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def show
render_identity
end
# Create an identity
#
# @body_parameter [string] installerId
# @body_parameter [string] password
#
# @response_status 201
# @response_root identity
# @response_class IdentitySerializer
def create
identity_created!
end
# Update an identity
#
# @body_parameter [string] password
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def update
identity.update(update_identity_params) ? render_identity : render_identity_errors
end
# Delete an identity
#
# @response_status 204
def destroy
identity.destroy
head(:no_content)
end
# Changes an identity's password
#
# @body_parameter [string] token to validate
# @body_parameter [string] password to assign
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def change_password
password_changed? ? render_identity : render_identity_errors
end
# Find an identity by installer_id
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def find_by_installer_id
@identity = Identity.find_by!(installer_id: params[:installer_id])
render_identity
end
# Renew and return the reset password token by installer_id
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def password_resetable
identity.password_resetable!
render_identity
end
# Validate a given reset password token by installer_id
#
# @body_parameter [string] token to validate
#
# @response_status 200
# @response_root identity
# @response_class IdentitySerializer
def validate_reset_password_token
render_identity
end
private
def password_changed?
identity.update(password: password)
end
# Filters
def check_installer
inactive_installer!(email) unless [identity, installer].all?(&:present?) && identity.installer_id == installer.id
end
def check_token
invalid_token! unless identity.password_resetable_with?(token)
end
def check_token_expiry
token_expired! unless PasswordResetable::Token.create(token).alive?
end
def create_identity
@identity = IdentitiesService.create(create_identity_params)
errors = identity_errors.messages
render_errors(errors) if errors.present?
end
def set_identity
@identity = Identity.find(params[:id])
end
def set_installer
super(params[:email])
end
def set_installer_identity
super(params[:id])
end
def set_password
@password = params[:password].presence
same_password! if identity.authenticate(password)
end
def set_token
@token = params[:token].presence
invalid_token! unless token
end
# Responses
def identity_created!
render json: identity, status: :created, location: identity
end
def invalid_token!
render_error("Invalid token!")
end
def render_identity
render json: identity
end
def render_identity_errors
render json: identity.errors, status: :unprocessable_entity
end
def same_password!
render_error("You cannot use the existing password")
end
def token_expired!
render_error("Token expired!")
end
def render_error(msg)
render json: { errors: msg }, status: :unprocessable_entity
end
alias_method :render_errors, :render_error
# Params
def create_identity_params
params.permit(:installer_id, :password)
end
def filter_params
params.permit(:installer_id)
end
def update_identity_params
params.permit(:password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment