class PlaceOS::Api::Users

Defined in:

placeos-rest-api/controllers/users.cr

Constant Summary

AFTER = {} of Nil => Nil
AROUND = {} of Nil => Nil
BEFORE = {set_request_id => {nil, nil}, authorize! => {nil, [root, mqtt_user, mqtt_access]}, set_user_id => {nil, [root, mqtt_user, mqtt_access]}, can_read => {[index, show], nil}, can_write => {[create, update, destroy, remove, revive, update_alt], nil}, user => {[destroy, update, revive, show], nil}, check_admin => {[index, destroy, create, revive], nil}, check_authorization => {[update, update_alt], nil}, ensure_json => {[update, update_alt], nil}, body => {[create, update, update_alt], nil}} of Nil => Nil
FORCE = {} of Nil => Nil
LOCAL_AFTER = {} of Nil => Nil

function => options

LOCAL_AROUND = {} of Nil => Nil

function => options

LOCAL_BEFORE = {can_read => {[index, show], nil}, can_write => {[create, update, destroy, remove, revive, update_alt], nil}, user => {[destroy, update, revive, show], nil}, check_admin => {[index, destroy, create, revive], nil}, check_authorization => {[update, update_alt], nil}, ensure_json => {[update, update_alt], nil}, body => {[create, update, update_alt], nil}} of Nil => Nil

function => options

LOCAL_FORCE = {} of Nil => Nil

function => options

LOCAL_RESCUE = {} of Nil => Nil

function => options

LOCAL_ROUTES = {current => {"get", "/current", nil, do begin render_json do |json| current_user.to_admin_json(json) end rescue e : RethinkORM::Error::DocumentNotFound head(:unauthorized) end end, false}, resource_token => {"post", "/resource_token", nil, do expired = true if access_token = current_user.access_token.presence if current_user.expires expires_at = Time.unix(current_user.expires_at.not_nil!) if 5.minutes.from_now < expires_at render(json: {token: access_token, expires: expires_at.to_unix}) end expired = 15.seconds.from_now > expires_at else render(json: {token: access_token}) end end if current_user.refresh_token.presence else head(:not_found) end authority = current_authority if authority else head(:not_found) end begin internals = authority.internals sso_strat = if sso_strat_id = internals["oauth-strategy"]?.try(&.as_s?) ::PlaceOS::Model::OAuthAuthentication.find(sso_strat_id) else ::PlaceOS::Model::OAuthAuthentication.collection_query do |table| table.get_all(authority.id, index: :authority_id) end.first? end if sso_strat else render(:not_found, text: "no oauth configuration found") end client_id = sso_strat.client_id client_secret = sso_strat.client_secret token_uri = URI.parse(sso_strat.token_url) token_host = token_uri.hostname.not_nil! token_path = token_uri.request_target oauth2_client = OAuth2::Client.new(token_host, client_id, client_secret, token_uri: token_path) token = oauth2_client.get_access_token_using_refresh_token(current_user.refresh_token, sso_strat.scope) current_user.access_token = token.access_token if token.refresh_token current_user.refresh_token = token.refresh_token end current_user.expires_at = Time.utc.to_unix + token.expires_in.not_nil! current_user.save! render(json: {token: current_user.access_token, expires: current_user.expires_at}) rescue error Log.warn(exception: error) do "failed refresh access token" end if !expired render(json: {token: current_user.access_token, expires: current_user.expires_at}) else raise(error) end end end, false}, update_alt => {"put", "/:id", nil, do update end, false}, revive => {"post", "/:id/revive", nil, do begin user.deleted = false save_and_respond(user) rescue e : Model::Error render_error(HTTP::Status::BAD_REQUEST, e.message) end end, false}, metadata => {"get", "/:id/metadata", nil, do parent_id = user.id.not_nil! render(json: Model::Metadata.build_metadata(parent_id, name)) end, false}, groups => {"get", "/groups", nil, do emails_param = required_param(emails) if (errors = self.class.validate_emails(emails_param)).empty? else return render_error(HTTP::Status::UNPROCESSABLE_ENTITY, errors.join(", ")) end render_json do |json| json.array do Model::User.find_by_emails(authority_id: current_user.authority_id.as(String), emails: emails_param).each(&.to_group_json(json)) end end end, false}} of Nil => Nil

function => options

LOCAL_SKIP = {} of Nil => Nil

function => options

Log = ::PlaceOS::Api::Log.for(self)
NAMESPACE = ["/api/engine/v2/users/"]

default namespace based on class

RESCUE = {Error::NoBody => {error_no_body, do |_error| message = "missing request body" Log.debug do message end return render_error(HTTP::Status::BAD_REQUEST, message) end}, JSON::SerializableError => {json_serializable_error, do |error| message = "Missing or extraneous properties in client JSON" Log.debug(exception: error) do message end if Api.production? return render_error(HTTP::Status::BAD_REQUEST, message) else return render_error(HTTP::Status::BAD_REQUEST, error.message, backtrace: error.backtrace?) end end}, JSON::ParseException => {json_parse_exception, do |error| message = "Failed to parse client JSON" Log.debug(exception: error) do message end if Api.production? return render_error(HTTP::Status::BAD_REQUEST, message) else return render_error(HTTP::Status::BAD_REQUEST, error.message, backtrace: error.backtrace?) end end}, Error::Unauthorized => {error_unauthorized, do |error| Log.debug do error.message end head(:unauthorized) end}, Error::Forbidden => {error_forbidden, do |error| Log.debug do error.message end head(:forbidden) end}, RethinkORM::Error::DocumentNotFound => {rethink_orm_error_document_not_found, do |error| Log.debug do error.message end head(:not_found) end}, RethinkORM::Error::DocumentInvalid => {rethink_orm_error_document_invalid, do |error| Log.debug do error.message end return render_error(HTTP::Status::UNPROCESSABLE_ENTITY, error.message) end}, Error::InvalidParams => {error_invalid_params, do |error| model_errors = error.params.errors.map(&.to_s) Log.debug(exception: error) do {message: "Invalid params", model_errors: model_errors} end return render_error(HTTP::Status::BAD_REQUEST, "Invalid params: #{model_errors.join(", ")}") end}} of Nil => Nil
ROUTE_RESOURCE = "users"
ROUTES = {current => {"get", "/current", nil, do begin render_json do |json| current_user.to_admin_json(json) end rescue e : RethinkORM::Error::DocumentNotFound head(:unauthorized) end end, false}, resource_token => {"post", "/resource_token", nil, do expired = true if access_token = current_user.access_token.presence if current_user.expires expires_at = Time.unix(current_user.expires_at.not_nil!) if 5.minutes.from_now < expires_at render(json: {token: access_token, expires: expires_at.to_unix}) end expired = 15.seconds.from_now > expires_at else render(json: {token: access_token}) end end if current_user.refresh_token.presence else head(:not_found) end authority = current_authority if authority else head(:not_found) end begin internals = authority.internals sso_strat = if sso_strat_id = internals["oauth-strategy"]?.try(&.as_s?) ::PlaceOS::Model::OAuthAuthentication.find(sso_strat_id) else ::PlaceOS::Model::OAuthAuthentication.collection_query do |table| table.get_all(authority.id, index: :authority_id) end.first? end if sso_strat else render(:not_found, text: "no oauth configuration found") end client_id = sso_strat.client_id client_secret = sso_strat.client_secret token_uri = URI.parse(sso_strat.token_url) token_host = token_uri.hostname.not_nil! token_path = token_uri.request_target oauth2_client = OAuth2::Client.new(token_host, client_id, client_secret, token_uri: token_path) token = oauth2_client.get_access_token_using_refresh_token(current_user.refresh_token, sso_strat.scope) current_user.access_token = token.access_token if token.refresh_token current_user.refresh_token = token.refresh_token end current_user.expires_at = Time.utc.to_unix + token.expires_in.not_nil! current_user.save! render(json: {token: current_user.access_token, expires: current_user.expires_at}) rescue error Log.warn(exception: error) do "failed refresh access token" end if !expired render(json: {token: current_user.access_token, expires: current_user.expires_at}) else raise(error) end end end, false}, update_alt => {"put", "/:id", nil, do update end, false}, revive => {"post", "/:id/revive", nil, do begin user.deleted = false save_and_respond(user) rescue e : Model::Error render_error(HTTP::Status::BAD_REQUEST, e.message) end end, false}, metadata => {"get", "/:id/metadata", nil, do parent_id = user.id.not_nil! render(json: Model::Metadata.build_metadata(parent_id, name)) end, false}, groups => {"get", "/groups", nil, do emails_param = required_param(emails) if (errors = self.class.validate_emails(emails_param)).empty? else return render_error(HTTP::Status::UNPROCESSABLE_ENTITY, errors.join(", ")) end render_json do |json| json.array do Model::User.find_by_emails(authority_id: current_user.authority_id.as(String), emails: emails_param).each(&.to_group_json(json)) end end end, false}, index => {"get", "/", nil, nil, false}, show => {"get", "/:id", nil, nil, false}, create => {"post", "/", nil, nil, false}, update => {"patch", "/:id", nil, nil, false}, destroy => {"delete", "/:id", nil, nil, false}} of Nil => Nil
SKIP = {} of Nil => Nil

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from class PlaceOS::Api::Application

body : IO body, boolean_param(key : String, default : Bool = false, allow_empty : Bool = false) : Bool boolean_param, paginate_results(elastic, query, route = base_route) paginate_results, request_id : String request_id, set_collection_headers(size : Int32, content_type : String) set_collection_headers, set_request_id set_request_id, set_user_id set_user_id

Class methods inherited from class PlaceOS::Api::Application

context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) context, core_discovery : Discovery::Core core_discovery

Class methods inherited from module PlaceOS::Api::Utils::Scopes

can_scope_access?(user_token : UserJWT, scope : String, access : Access) can_scope_access?, can_scopes_access!(user_token : UserJWT, scopes : Enumerable(String), access : Access) can_scopes_access!

Instance methods inherited from module PlaceOS::Api::Utils::CurrentUser

authorize! : Model::UserJWT authorize!, check_admin check_admin, check_oauth_scope check_oauth_scope, check_support check_support, current_authority : Model::Authority | Nil current_authority, current_user : Model::User current_user, is_admin? is_admin?, is_support? is_support?, user_token : Model::UserJWT user_token

Instance methods inherited from module PlaceOS::Api::Utils::Responders

handle_execute_error(error : Driver::Proxy::RemoteDriver::Error, respond : Bool = true) handle_execute_error, render_error(status : HTTP::Status, message : String | Nil, **additional) render_error, restrict_attributes(model, only : Array(String) | Nil = nil, except : Array(String) | Nil = nil, fields : Hash | Nil = nil) : Hash restrict_attributes, save_and_respond(resource, &)
save_and_respond(resource)
save_and_respond
, save_and_status(resource) save_and_status, with_fields(model, fields) : Hash with_fields

Class Method Detail

def self.__init_routes__(router) #

Routes call the functions generated above


def self.__route_list__ #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.base_route #

Helper for obtaining base route


def self.context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) #

[View source]
def self.create(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.current(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Helper methods for performing redirect_to calls


def self.delete__api_engine_v2_users___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.destroy(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_users__(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_users___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_users___id_metadata(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_users__current(context, head_request) #

Generate functions for each route


def self.get__api_engine_v2_users__groups(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.groups(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.index(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.metadata(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.patch__api_engine_v2_users___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_users__(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_users___id_revive(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_users__resource_token(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.put__api_engine_v2_users___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.resource_token(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.revive(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.show(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.update(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.update_alt(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.validate_emails(emails) : Array(String) #

[View source]

Instance Method Detail

def authority_id : String | Nil #

[View source]
def base_route(*args, **options) #

Helper for obtaining base route


def base_route(*args, **options, &) #

Helper for obtaining base route


def create #

[View source]
def current #

Render the current user


[View source]
def destroy #

Destroy user, revoke authentication.


[View source]
def emails : Array(String) | Nil #

[View source]
def error_forbidden(error) #

def error_invalid_params(error) #

def error_no_body(_error) #

Create functions as required for errors Skip the generating methods for existing handlers


def error_unauthorized(error) #

def groups #

Params

  • #emails: comma-seperated list of emails required

Returns

  • [{id: "<user-id>", groups: ["<group>"]}]

[View source]
def include_deleted? : Bool #

[View source]
def index #

[View source]
def json_parse_exception(error) #

def json_serializable_error(error) #

def metadata #

[View source]
def name : String | Nil #

[View source]
def resource_token #

Obtain a token to the current users SSO resource


[View source]
def rethink_orm_error_document_invalid(error) #

def rethink_orm_error_document_not_found(error) #

def revive #

[View source]
def show #

[View source]
def update #

[View source]
def update_alt #

def user : Model::User #

[View source]

Macro Detail

macro __inherit_after_filters__ #

macro __inherit_around_filters__ #

macro __inherit_before_filters__ #

macro __inherit_force_filters__ #

macro __inherit_rescue_filters__ #

macro __inherit_routes_filters__ #

macro __inherit_skip_filters__ #

macro generate_scope_check(*scopes) #