Class: JsonCrudApi::Service
- Inherits:
-
Object
- Object
- JsonCrudApi::Service
- Defined in:
- lib/json-crud-api/service.rb
Instance Attribute Summary collapse
-
#log_service ⇒ Object
Returns the value of attribute log_service.
-
#model ⇒ Object
Returns the value of attribute model.
-
#scope_map ⇒ Object
Returns the value of attribute scope_map.
-
#user ⇒ Object
Returns the value of attribute user.
-
#user_scopes ⇒ Object
Returns the value of attribute user_scopes.
Instance Method Summary collapse
-
#create(params) ⇒ Object
Create a record with the given attributes.
-
#delete(key) ⇒ Object
Delete a record with the given key Returns false if the record does not exist.
-
#exists?(key) ⇒ Boolean
Determine if a record with the given key exists.
-
#get(key) ⇒ Object
Get the first record with the given key.
-
#get_all ⇒ Object
Get all records.
-
#initialize(options) ⇒ Service
constructor
A new instance of Service.
-
#set_user(user) ⇒ Object
Set the current user.
-
#set_user_scopes(user_scopes) ⇒ Object
Set the current user scopes.
-
#update(key, params) ⇒ Object
Update a record with the given key with the given attributes Returns false if the record does not exist.
-
#user_authorized_for?(operation) ⇒ Boolean
Determine if the current user is authorized for the given operation.
Constructor Details
#initialize(options) ⇒ Service
Returns a new instance of Service.
8 9 10 11 12 13 14 |
# File 'lib/json-crud-api/service.rb', line 8 def initialize() @log_service = [:log_service] @model = [:model] @scope_map = [:scope_map] @user = nil @user_scopes = nil end |
Instance Attribute Details
#log_service ⇒ Object
Returns the value of attribute log_service.
6 7 8 |
# File 'lib/json-crud-api/service.rb', line 6 def log_service @log_service end |
#model ⇒ Object
Returns the value of attribute model.
6 7 8 |
# File 'lib/json-crud-api/service.rb', line 6 def model @model end |
#scope_map ⇒ Object
Returns the value of attribute scope_map.
6 7 8 |
# File 'lib/json-crud-api/service.rb', line 6 def scope_map @scope_map end |
#user ⇒ Object
Returns the value of attribute user.
6 7 8 |
# File 'lib/json-crud-api/service.rb', line 6 def user @user end |
#user_scopes ⇒ Object
Returns the value of attribute user_scopes.
6 7 8 |
# File 'lib/json-crud-api/service.rb', line 6 def user_scopes @user_scopes end |
Instance Method Details
#create(params) ⇒ Object
Create a record with the given attributes
17 18 19 |
# File 'lib/json-crud-api/service.rb', line 17 def create(params) @model.create(params) end |
#delete(key) ⇒ Object
Delete a record with the given key Returns false if the record does not exist.
47 48 49 50 51 52 |
# File 'lib/json-crud-api/service.rb', line 47 def delete(key) record = get(key) return false if record.nil? record.destroy end |
#exists?(key) ⇒ Boolean
Determine if a record with the given key exists
22 23 24 |
# File 'lib/json-crud-api/service.rb', line 22 def exists?(key) @model.all(@model.key.first.name => key).count > 0 end |
#get(key) ⇒ Object
Get the first record with the given key
32 33 34 |
# File 'lib/json-crud-api/service.rb', line 32 def get(key) @model.first(@model.key.first.name => key) end |
#get_all ⇒ Object
Get all records
27 28 29 |
# File 'lib/json-crud-api/service.rb', line 27 def get_all @model.all end |
#set_user(user) ⇒ Object
Set the current user
55 56 57 58 |
# File 'lib/json-crud-api/service.rb', line 55 def set_user(user) @user = user set_user_scopes(user[:scopes]) unless @user.nil? end |
#set_user_scopes(user_scopes) ⇒ Object
Set the current user scopes
61 62 63 |
# File 'lib/json-crud-api/service.rb', line 61 def set_user_scopes(user_scopes) @user_scopes = user_scopes end |
#update(key, params) ⇒ Object
Update a record with the given key with the given attributes Returns false if the record does not exist.
38 39 40 41 42 43 |
# File 'lib/json-crud-api/service.rb', line 38 def update(key, params) record = get(key) return false if record.nil? record.update(params) end |
#user_authorized_for?(operation) ⇒ Boolean
Determine if the current user is authorized for the given operation
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/json-crud-api/service.rb', line 66 def (operation) # Auth is disabled if scope map is nil return true if @scope_map.nil? # Auth succeeds if there is no map for this operation return true if @scope_map[operation].nil? # Auth fails if user is not logged in return false if @user.nil? # Auth fails if user has no scopes return false if @user_scopes.nil? or @user_scopes.empty? if @scope_map[operation].is_a?(Array) # Auth succeeds if the intersection of allowed scopes and mapped scopes is non-empty. return !((@scope_map[operation] & @user_scopes).empty?) end # Auth succeeds if the mapped scope is singular and the user posesses it @user_scopes.include?(@scope_map[operation]) end |