Skip to content
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

Add missing User Management API endpoints #169

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
129 changes: 127 additions & 2 deletions lib/auth0/api/v2/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def create_user(name, options = {})

# Delete all users - USE WITH CAUTION
# @see https://auth0.com/docs/api/v2#!/Users/delete_users
# TODO: Deprecate, no longer provided
def delete_users
delete(users_path)
end
Expand Down Expand Up @@ -96,7 +97,7 @@ def delete_user(user_id)
# If your are updating email or phone_number you need to specify the connection and the client_id properties.
# @see https://auth0.com/docs/api/v2#!/Users/patch_users_by_id
# @param user_id [string] The user_id of the user to update.
# @param body [hash] The optional parametes to update.
# @param body [hash] The optional parameters to update.
#
# @return [json] Returns the updated user.
def patch_user(user_id, body)
Expand Down Expand Up @@ -168,7 +169,6 @@ def user_logs(user_id, options = {})
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
path = "#{users_path}/#{user_id}/logs"
request_params = {
user_id: user_id,
per_page: options.fetch(:per_page, nil),
page: options.fetch(:page, nil),
include_totals: options.fetch(:include_totals, nil),
Expand All @@ -185,12 +185,137 @@ def user_logs(user_id, options = {})
end
alias get_user_log_events user_logs

# Get all roles assigned to a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/get_user_roles
#
# @param user_id [string] The user_id of the roles to retrieve.
# @param options [hash]
# * :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.
# * :page [integer] The page number. Zero based.
# * :include_totals [boolean] True if a query summary must be included in the result.
# * :sort [string] The field to use for sorting. 1 == ascending and -1 == descending.
#
# @return [json] Returns roles for the given user_id.
def get_roles(user_id, options = {})
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
path = "#{users_path}/#{user_id}/roles"
request_params = {
per_page: options.fetch(:per_page, nil),
page: options.fetch(:page, nil),
include_totals: options.fetch(:include_totals, nil)
}
get(path, request_params)
end

# Remove one or more roles from a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/delete_user_roles
#
# @param user_id [string] The user_id of the roles to remove.
# @param roles [array] An array of role names to remove.
def remove_roles(user_id, roles)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
validate_roles_array roles
path = "#{users_path}/#{user_id}/roles"
delete(path, { roles: roles })
end

# Add one or more roles to a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/post_user_roles
#
# @param user_id [string] The user_id of the roles to add.
# @param roles [array] An array of role names to add.
def add_roles(user_id, roles)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
validate_roles_array roles
path = "#{users_path}/#{user_id}/roles"
post(path, { roles: roles })
end

# Get all Guardian enrollments for a specific user
# @see https://auth0.com/docs/api/management/v2#!/Users/get_enrollments
#
# @param user_id [string] The user_id of the enrollments to get.
#
# @return [json] Returns Guardian enrollments for the given user_id.
def get_enrollments(user_id)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
get "#{users_path}/#{user_id}/enrollments"
end

# Get all permissions for a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/get_permissions
#
# @param user_id [string] The user_id of the permissions to get.
#
# @return [json] Returns permissions for the given user_id.
def get_permissions(user_id)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
get "#{users_path}/#{user_id}/permissions"
end

# Remove one or more permissions from a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/delete_permissions
#
# @param user_id [string] The user_id of the permissions to remove.
# @param permissions [array] An array of Permission structs to remove.
def remove_permissions(user_id, permissions)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
permissions = validate_permissions_array permissions
delete("#{users_path}/#{user_id}/permissions", permissions)
end

# Add one or more permissions from a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/post_permissions
#
# @param user_id [string] The user_id of the permissions to add.
# @param permissions [array] An array of Permission structs to add.
def add_permissions(user_id, permissions)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
permissions = validate_permissions_array permissions
post("#{users_path}/#{user_id}/permissions", permissions)
end

# Remove the current Guardian recovery code and generates and returns a new one.
# @see https://auth0.com/docs/api/management/v2#!/Users/post_recovery_code_regeneration
#
# @param user_id [string] The user_id of the recovery codes to regenerate.
def generate_recovery_code(user_id)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
post "#{users_path}/#{user_id}/recovery-code-generation"
end

# Invalidate all remembered browsers for all authentication factors for a specific user.
# @see https://auth0.com/docs/api/management/v2#!/Users/post_invalidate_remember_browser
#
# @param user_id [string] The user_id of the browsers to invalidate.
def invalidate_browsers(user_id)
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
post "#{users_path}/#{user_id}/multifactor/actions/invalidate-remember-browser"
end

private

# Users API path
def users_path
@users_path ||= '/api/v2/users'
end

# Check a roles array
def validate_roles_array(roles)
raise Auth0::InvalidParameter, 'Must supply an array of role names' unless roles.kind_of?(Array)
raise Auth0::InvalidParameter, 'Must supply an array of role names' if roles.empty?
raise Auth0::InvalidParameter, 'All role names must be strings' unless roles.all? {|role| role.is_a? String}
end

# Check a permissions array
def validate_permissions_array(permissions)
raise Auth0::InvalidParameter, 'Must supply an array of Permissions' unless permissions.kind_of?(Array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalized Permissions here too should be permissions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆

raise Auth0::InvalidParameter, 'Must supply an array of Permissions' if permissions.empty?
raise Auth0::InvalidParameter, 'All array elements must be Permissions' unless permissions.all? do |permission|
permission.kind_of? Permission
end
permissions.map { |permission| permission.to_h }
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/auth0/mixins/permission_struct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Permission = Struct.new :permission_name, :resource_server_identifier do

end
Loading