virtuatable-core 1.0.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/core/controllers/base.rb +63 -0
- data/lib/core/controllers.rb +9 -0
- data/lib/core/helpers/accounts.rb +22 -0
- data/lib/core/helpers/applications.rb +26 -0
- data/lib/core/helpers/declarators.rb +83 -0
- data/lib/core/helpers/errors/bad_request.rb +15 -0
- data/lib/core/helpers/errors/base.rb +36 -0
- data/lib/core/helpers/errors/forbidden.rb +15 -0
- data/lib/core/helpers/errors/not_found.rb +15 -0
- data/lib/core/helpers/errors.rb +48 -0
- data/lib/core/helpers/fields.rb +35 -0
- data/lib/core/helpers/parameters.rb +25 -0
- data/lib/core/helpers/responses.rb +50 -0
- data/lib/core/helpers/routes.rb +21 -0
- data/lib/core/helpers/sessions.rb +30 -0
- data/lib/core/helpers.rb +18 -0
- data/lib/core/models/account.rb +2 -5
- data/lib/core/models/authentication/session.rb +2 -0
- data/lib/core/models/campaign.rb +7 -1
- data/lib/core/models/campaigns/invitation.rb +2 -0
- data/lib/core/models/campaigns/map.rb +37 -0
- data/lib/core/models/campaigns/tag.rb +2 -0
- data/lib/core/models/campaigns/token.rb +31 -0
- data/lib/core/models/campaigns/token_position.rb +39 -0
- data/lib/core/models/campaigns.rb +3 -0
- data/lib/core/models/chatrooms/campaign.rb +3 -0
- data/lib/core/models/chatrooms/message.rb +3 -1
- data/lib/core/models/concerns/activable.rb +1 -1
- data/lib/core/models/concerns/premiumable.rb +1 -1
- data/lib/core/models/concerns.rb +0 -1
- data/lib/core/models/files/document.rb +2 -0
- data/lib/core/models/files/permission.rb +2 -0
- data/lib/core/models/notification.rb +1 -1
- data/lib/core/models/oauth/access_token.rb +2 -0
- data/lib/core/models/oauth/application.rb +3 -1
- data/lib/core/models/oauth/authorization.rb +2 -0
- data/lib/core/models/oauth/refresh_token.rb +2 -0
- data/lib/core/models/permissions/category.rb +2 -0
- data/lib/core/models/permissions/group.rb +5 -3
- data/lib/core/models/permissions/right.rb +2 -0
- data/lib/core/models/{monitoring → permissions}/route.rb +7 -16
- data/lib/core/models/permissions.rb +1 -0
- data/lib/core/models/ruleset.rb +2 -0
- data/lib/core/models.rb +0 -3
- data/lib/core/services/accounts.rb +16 -0
- data/lib/core/services/base.rb +11 -0
- data/lib/core/services/registry.rb +15 -0
- data/lib/core/services/sessions.rb +33 -0
- data/lib/core/services.rb +8 -0
- data/lib/core/version.rb +1 -1
- data/lib/core.rb +3 -0
- metadata +84 -43
- data/lib/core/models/chatrooms/conversation.rb +0 -9
- data/lib/core/models/chatrooms/membership.rb +0 -17
- data/lib/core/models/concerns/diagnosticable.rb +0 -24
- data/lib/core/models/decorators/errors/env_variable_missing.rb +0 -16
- data/lib/core/models/decorators/errors.rb +0 -11
- data/lib/core/models/decorators/gateway.rb +0 -111
- data/lib/core/models/factories/errors/gateway_not_found.rb +0 -16
- data/lib/core/models/factories/errors.rb +0 -11
- data/lib/core/models/factories.rb +0 -10
- data/lib/core/models/monitoring/service.rb +0 -33
- data/lib/core/models/monitoring.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfc5218a02310414bc7961f93e97d05bb37cb73b9d4868fa8c83e6666485447a
|
4
|
+
data.tar.gz: c83d949e50ae999b2182fd7c4678b9b45a5418c25d750d859d43f4f41713029e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34d89edd05d094d7db645e4403060bbbe091e3e4c77733285cd34fb7d4ab3890f28a03427bb270195c5422f4ea6235140a5693d5a112b3178f25d7a1fe9e8314
|
7
|
+
data.tar.gz: c5c6871afefd3a2dd445ffaf060c59cb676ed54a720ae26c401af4a5b62e49be4969baac10eebea8bd46ded9a8835249b62168c6ef1338560f00f6d190d288cc
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'sinatra/config_file'
|
3
|
+
require 'sinatra/custom_logger'
|
4
|
+
|
5
|
+
module Core
|
6
|
+
module Controllers
|
7
|
+
# This class represents a base controller for the system, giving access
|
8
|
+
# to checking methods for sessions, gateways, applications, etc.
|
9
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
10
|
+
class Base < Sinatra::Base
|
11
|
+
register Sinatra::ConfigFile
|
12
|
+
helpers Sinatra::CustomLogger
|
13
|
+
# Includes the custom errors throwers and responses helpers.
|
14
|
+
include Core::Helpers::Errors
|
15
|
+
include Core::Helpers::Responses
|
16
|
+
# Includes the checking methods for sessions.
|
17
|
+
include Core::Helpers::Sessions
|
18
|
+
# Include the checkers and getters for OAuth apps
|
19
|
+
include Core::Helpers::Applications
|
20
|
+
# Include checkers for field requirement and check
|
21
|
+
include Core::Helpers::Fields
|
22
|
+
# Include the getter for the currently requested route.
|
23
|
+
include Core::Helpers::Routes
|
24
|
+
# Include the getter and checkers for accounts.
|
25
|
+
include Core::Helpers::Accounts
|
26
|
+
# Include the loading of the parameters from the JSON body
|
27
|
+
include Core::Helpers::Parameters
|
28
|
+
# This module is extended, not included, because it provides routes
|
29
|
+
# declaration methods used in class declarations.
|
30
|
+
extend Core::Helpers::Declarators
|
31
|
+
|
32
|
+
configure do
|
33
|
+
set :logger, Logger.new(STDOUT)
|
34
|
+
logger.level = Logger::ERROR if ENV['RACK_ENV'] == 'test'
|
35
|
+
# This configuration options allow the error handler to work in tests.
|
36
|
+
set :show_exceptions, false
|
37
|
+
set :raise_errors, false
|
38
|
+
end
|
39
|
+
|
40
|
+
error Mongoid::Errors::Validations do |errors|
|
41
|
+
key = errors.document.errors.messages.keys.first
|
42
|
+
message = errors.document.errors.messages[key][0]
|
43
|
+
api_bad_request key, message: message
|
44
|
+
end
|
45
|
+
|
46
|
+
error Core::Helpers::Errors::NotFound do |exception|
|
47
|
+
api_not_found exception.message
|
48
|
+
end
|
49
|
+
|
50
|
+
error Core::Helpers::Errors::BadRequest do |exception|
|
51
|
+
api_bad_request exception.message
|
52
|
+
end
|
53
|
+
|
54
|
+
error Core::Helpers::Errors::Forbidden do |exception|
|
55
|
+
api_forbidden exception.message
|
56
|
+
end
|
57
|
+
|
58
|
+
error StandardError do |error|
|
59
|
+
api_error 500, "unknown_field.#{error.class.name}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# These helpers provide methods used to get and check accounts.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Accounts
|
8
|
+
# Raises a bad request error if the account if not found.
|
9
|
+
# @raise [Virtuatable::API::Errors::BadRequest] the error raised when the account is not found.
|
10
|
+
def account
|
11
|
+
return @account unless @account.nil?
|
12
|
+
|
13
|
+
session_id_required if !respond_to?(:session) || session.nil?
|
14
|
+
@account = session.account
|
15
|
+
end
|
16
|
+
|
17
|
+
def account_id_not_found
|
18
|
+
api_bad_request('session_id.required')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# Helpers to get and check OAuth applications connecting the the application.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Applications
|
8
|
+
# Looks for the application sending the API's request, and raises error if not found.
|
9
|
+
# @param [Core::Models::OAuth::Application] the application requesting the service.
|
10
|
+
def application(premium: false)
|
11
|
+
return @application unless @application.nil?
|
12
|
+
|
13
|
+
check_presence 'app_key'
|
14
|
+
@application = application_model.find_by(key: params['app_key'])
|
15
|
+
api_not_found 'app_key.unknown' if @application.nil?
|
16
|
+
api_forbidden 'app_key.forbidden' if premium && !@application.premium
|
17
|
+
|
18
|
+
@application
|
19
|
+
end
|
20
|
+
|
21
|
+
def application_model
|
22
|
+
Core::Models::OAuth::Application
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# This helpers module is a bit larger than the others as it provides methods
|
6
|
+
# to declare routes whithin a service, performing needed checks and filters.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
module Declarators
|
9
|
+
# @!attribute [r] routes
|
10
|
+
# @return [Array<Core::Models::Permissions::Route>] the currently declared routes.
|
11
|
+
attr_reader :api_routes
|
12
|
+
|
13
|
+
# Main method to declare new routes, persisting them in the database and
|
14
|
+
# declaring it in the Sinatra application with the needed before checks.
|
15
|
+
#
|
16
|
+
# @param verb [String] the HTTP method for the route.
|
17
|
+
# @param path [String] the whole URI with parameters for the route.
|
18
|
+
# @param options [Hash] the additional options for the route.
|
19
|
+
def api_route(verb, path, options: {}, &block)
|
20
|
+
options = default_options.merge(options)
|
21
|
+
route = add_route(verb: verb, path: path, options: options)
|
22
|
+
|
23
|
+
# TODO : do everything in the #send itself to avoid
|
24
|
+
# route reload issues when premium is changed. It will
|
25
|
+
# add some treatments but avoid many problems if route.premium
|
26
|
+
send(route.verb, route.path) do
|
27
|
+
application(premium: current_route.premium)
|
28
|
+
session if current_route.authenticated
|
29
|
+
instance_eval(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Add a route to the database, then to the routes array.
|
34
|
+
# @param verb [String] the HTTP method used to request this route.
|
35
|
+
# @param path [String] the path used to request this route.
|
36
|
+
# @return [Core::Models::Permissions::Route] the created route.
|
37
|
+
def add_route(verb:, path:, options:)
|
38
|
+
route = Core::Models::Permissions::Route.find_or_create_by!(
|
39
|
+
path: path,
|
40
|
+
verb: verb.downcase,
|
41
|
+
premium: options[:premium],
|
42
|
+
authenticated: options[:authenticated]
|
43
|
+
)
|
44
|
+
api_routes.nil? ? @api_routes = [route] : push_route(route)
|
45
|
+
add_permissions(route)
|
46
|
+
route
|
47
|
+
end
|
48
|
+
|
49
|
+
# Pushes the route in the api routes list, by creating it if needed
|
50
|
+
# @param route [Core::Models::Permissions::Route] the route to push in the list of routes.
|
51
|
+
def push_route(route)
|
52
|
+
@api_routes << route if api_routes.none? do |tmp_route|
|
53
|
+
route.id == tmp_route.id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Add the default access permissions to a route. Any group tagged superuser
|
58
|
+
# can automatically access any newly declared_route.
|
59
|
+
# params route [Core::Models::Permissions::Route] the route to add the permissions to.
|
60
|
+
def add_permissions(route)
|
61
|
+
groups = Core::Models::Permissions::Group.where(is_superuser: true)
|
62
|
+
groups.each do |group|
|
63
|
+
unless route.groups.where(id: group.id).exists?
|
64
|
+
route.groups << group
|
65
|
+
route.save!
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# The default options for a route, being the most used value for each key.
|
71
|
+
# @return [Hash] the default options as a hash.
|
72
|
+
def default_options
|
73
|
+
{
|
74
|
+
# If TRUE the application MUST be premium to access the route.
|
75
|
+
# Mainly used to protect administration routes against illegal accesses.
|
76
|
+
premium: false,
|
77
|
+
# If TRUE the user MUST be authenticated to access the route.
|
78
|
+
authenticated: true
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
module Errors
|
6
|
+
# A bad request error is raised when the data given to a model makes this model invalid.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
class BadRequest < Core::Helpers::Errors::Base
|
9
|
+
def initialize(field:, error:)
|
10
|
+
super(field: field, error: error, status: 400)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
module Errors
|
6
|
+
# Standard class parent to all specialized http errors.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
class Base < StandardError
|
9
|
+
# @!attribute [rw] field
|
10
|
+
# @return [String, Symbol] the name of the field in error in the model.
|
11
|
+
attr_accessor :field
|
12
|
+
# @!attribute [rw] action
|
13
|
+
# @return [String] the name of the action the user was trying to perform on the model (often crate or update).
|
14
|
+
attr_accessor :action
|
15
|
+
# @attribute [rw] error
|
16
|
+
# @return [String] the label of the error returned by the model.
|
17
|
+
attr_accessor :error
|
18
|
+
# @attribute [rw] status
|
19
|
+
# @return [Integer] the HTTP status code as a number (eg: 400, 422 or 500)
|
20
|
+
attr_accessor :status
|
21
|
+
|
22
|
+
def initialize(field:, error:, status:)
|
23
|
+
@field = field.to_s
|
24
|
+
@error = error
|
25
|
+
@status = status
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the formatted message for this exception.
|
29
|
+
# @return [String] a message indicating what field fails, and why.
|
30
|
+
def message
|
31
|
+
"#{field}.#{error}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
module Errors
|
6
|
+
# A forbidden error occurs when a user tries to perform an action he's not allowed to.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
class Forbidden < Core::Helpers::Errors::Base
|
9
|
+
def initialize(field:, error:)
|
10
|
+
super(field: field, error: error, status: 403)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
module Errors
|
6
|
+
# A not found error occurs when a user tries to reach a resource that does not exist.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
class NotFound < Core::Helpers::Errors::Base
|
9
|
+
def initialize(field:, error:)
|
10
|
+
super(field: field, error: error, status: 404)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# This module defines method to raise HTTP errors in the routes easily.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Errors
|
8
|
+
autoload :Base, 'core/helpers/errors/base'
|
9
|
+
autoload :BadRequest, 'core/helpers/errors/bad_request'
|
10
|
+
autoload :Forbidden, 'core/helpers/errors/forbidden'
|
11
|
+
autoload :NotFound, 'core/helpers/errors/not_found'
|
12
|
+
|
13
|
+
# Stops the executing and raises an HTTP error in the route.
|
14
|
+
# The message MUST be of the for <field>.<error> to be correctly parsed.
|
15
|
+
# The action is automatically parsed from the route call and added.
|
16
|
+
#
|
17
|
+
# @param status [Integer] the HTTP status code the response will have
|
18
|
+
# @param message [String] the raw message to split and format as body.
|
19
|
+
def api_error(status, message)
|
20
|
+
field, error = message.split('.')
|
21
|
+
docs = settings.errors.try(field).try(error)
|
22
|
+
errors = { status: status, field: field, error: error, docs: docs }
|
23
|
+
halt status, errors.to_json
|
24
|
+
end
|
25
|
+
|
26
|
+
# Stops the execution to return a NOT FOUND response.
|
27
|
+
# @param field [String] the field in params concerned by the error.
|
28
|
+
# @param message [String] the message if different of "unknown".
|
29
|
+
def api_not_found(field, message: 'unknown')
|
30
|
+
api_error 404, "#{field}.#{message}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Stops the execution to return a BAD REQUEST response.
|
34
|
+
# @param field [String] the field in params concerned by the error.
|
35
|
+
# @param message [String] the message if different of "required".
|
36
|
+
def api_bad_request(field, message: 'required')
|
37
|
+
api_error 400, "#{field}.#{message}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Stops the execution to return a FORBIDDEN response.
|
41
|
+
# @param field [String] the field in params concerned by the error.
|
42
|
+
# @param message [String] the message if different of "forbidden".
|
43
|
+
def api_forbidden(field, message: 'forbidden')
|
44
|
+
api_error 403, "#{field}.#{message}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# Helpers for the parameters of a request.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Fields
|
8
|
+
# Checks the presence of several fields given as parameters and halts the execution if it's not present.
|
9
|
+
# @param fields [Array<String>] an array of fields names to search in the parameters
|
10
|
+
def check_presence(*fields)
|
11
|
+
fields.each do |field|
|
12
|
+
api_bad_request "#{field}.required" unless field_defined?(field)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Checks the presence of either fields given in parameters.
|
17
|
+
# It halts with an error only if ALL parameters are not given.
|
18
|
+
#
|
19
|
+
# @param fields [Array<String>] an array of fields names to search in the parameters
|
20
|
+
# @param key [String] the key to search in the errors configuration file.
|
21
|
+
def check_either_presence(*fields, key:)
|
22
|
+
api_bad_request "#{key}.required" if fields.none? do |field|
|
23
|
+
field_defined?(field)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Checks if a given field is defined in the params
|
28
|
+
# @param field [String] the name of the field to check in the params
|
29
|
+
# @return [Boolean] TRUE if the field exists, FALSE otherwise.
|
30
|
+
def field_defined?(field)
|
31
|
+
!params.nil? && params.key?(field) && params[field] != ''
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# Helpers to correctly build the parameters hash, even from the JSON body.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Parameters
|
8
|
+
# Returns the parameters depending on whether the request has a body
|
9
|
+
# or not. If it has a body, it parses it, otherwise it just returns the params.
|
10
|
+
# @return [Hash] the parameters sent with the request.
|
11
|
+
def params
|
12
|
+
super.merge(body_params)
|
13
|
+
end
|
14
|
+
|
15
|
+
# The parameters from the JSON body if it is sent.
|
16
|
+
# @return [Hash] the JSON body parsed as a dictionary.
|
17
|
+
def body_params
|
18
|
+
request.body.rewind
|
19
|
+
JSON.parse(request.body.read.to_s)
|
20
|
+
rescue JSON::ParserError
|
21
|
+
{}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# Modules holding the responses that are NOT errors.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Responses
|
8
|
+
# Builds a list of items as a standard API response.
|
9
|
+
# The response will be a JSON hash containing two keys :
|
10
|
+
# - :count will hold the number of items displayed in the list
|
11
|
+
# - :items will hold the list of items.
|
12
|
+
# @param items [Array] the items to format as a standard API response.
|
13
|
+
def api_list(items)
|
14
|
+
halt 200, {
|
15
|
+
count: items.count,
|
16
|
+
items: items.map { |item| enhanced_h(item) }
|
17
|
+
}.to_json
|
18
|
+
end
|
19
|
+
|
20
|
+
# Displays a creation standard response,
|
21
|
+
# returning the informations about the created item.
|
22
|
+
# @param item [Object] any object that responds to #to_h to display to the user.
|
23
|
+
def api_created(item)
|
24
|
+
halt 201, enhanced_json(item)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Displays an item with the standards of the API.
|
28
|
+
# @param item [Object] the item to display as a JSON formatted hash.
|
29
|
+
def api_item(item)
|
30
|
+
halt 200, enhanced_json(item)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Displays a message with a 200 status code
|
34
|
+
# @param message [String] the message to display with the API standards.
|
35
|
+
def api_ok(message)
|
36
|
+
api_item message: message
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def enhanced_h(item)
|
42
|
+
(item.respond_to?(:enhance) ? item.enhance : item).to_h
|
43
|
+
end
|
44
|
+
|
45
|
+
def enhanced_json(item)
|
46
|
+
enhanced_h(item).to_json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# This module provides the #current_route method to get the current
|
6
|
+
# Core::Models::Monitoring::Route object from whithin sinatra routes.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
module Routes
|
9
|
+
# The currently requested API route, used to see inside the block
|
10
|
+
# if the route is premium or not, authenticated or not.
|
11
|
+
# @return [Core::Models::Monitoring::Route] the currently requested route.
|
12
|
+
def current_route
|
13
|
+
splitted = request.env['sinatra.route'].split(' ')
|
14
|
+
verb = splitted.first.downcase
|
15
|
+
self.class.api_routes.find do |route|
|
16
|
+
route.verb == verb && route.path == splitted.last
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# This helper gives access to methods about user's session on the API.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Sessions
|
8
|
+
# Checks the session of the user requesting the API and returns an error
|
9
|
+
# if it either not exists with the given token, or the token is not given.
|
10
|
+
#
|
11
|
+
# @raise [Virtuatable::API::Errors::NotFound] if the session is not found
|
12
|
+
# or the token not given in the parameters of the request.
|
13
|
+
# @raise [Virtuatable::API::Errors::BadRequest] if the session token is
|
14
|
+
# not correctly given in the parameters.
|
15
|
+
#
|
16
|
+
# @return [Core::Models::Authentication::Session] the current session of the user.
|
17
|
+
def session
|
18
|
+
return @session unless @session.nil?
|
19
|
+
|
20
|
+
check_presence 'session_id'
|
21
|
+
@session = session_model.find_by(token: params['session_id'])
|
22
|
+
@session.nil? ? api_not_found('session_id.unknown') : @session
|
23
|
+
end
|
24
|
+
|
25
|
+
def session_model
|
26
|
+
Core::Models::Authentication::Session
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/core/helpers.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
# The helpers are used inside the controllers to dynamically
|
5
|
+
# add features and functions.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Helpers
|
8
|
+
autoload :Accounts, 'core/helpers/accounts'
|
9
|
+
autoload :Applications, 'core/helpers/applications'
|
10
|
+
autoload :Declarators, 'core/helpers/declarators'
|
11
|
+
autoload :Errors, 'core/helpers/errors'
|
12
|
+
autoload :Fields, 'core/helpers/fields'
|
13
|
+
autoload :Parameters, 'core/helpers/parameters'
|
14
|
+
autoload :Responses, 'core/helpers/responses'
|
15
|
+
autoload :Routes, 'core/helpers/routes'
|
16
|
+
autoload :Sessions, 'core/helpers/sessions'
|
17
|
+
end
|
18
|
+
end
|
data/lib/core/models/account.rb
CHANGED
@@ -8,6 +8,8 @@ module Core
|
|
8
8
|
include ActiveModel::SecurePassword
|
9
9
|
include Core::Models::Concerns::Enumerable
|
10
10
|
|
11
|
+
store_in collection: 'accounts'
|
12
|
+
|
11
13
|
# @!attribute [rw] username
|
12
14
|
# @return [String] the nickname the user chose at subscription, must be given, unique, and 6 or more characters long.
|
13
15
|
field :username, type: String
|
@@ -46,9 +48,6 @@ module Core
|
|
46
48
|
# @!attribute [rw] authorizations
|
47
49
|
# @return [Array<Core::Models::OAuth::Authorization>] the authorization issued by this account to third-party applications to access its data.
|
48
50
|
has_many :authorizations, class_name: 'Core::Models::OAuth::Authorization', inverse_of: :account
|
49
|
-
# @!attribute [rw] services
|
50
|
-
# @return [Array<Core::Models::Monitoring::Service>] the services created by this user.
|
51
|
-
has_many :services, class_name: 'Core::Models::Monitoring::Service', inverse_of: :creator
|
52
51
|
# @!attribute [rw] sessions
|
53
52
|
# @return [Array<Core::Models::Authentication::Session>] the sessions on which this account is, or has been logged in.
|
54
53
|
has_many :sessions, class_name: 'Core::Models::Authentication::Session', inverse_of: :account
|
@@ -65,8 +64,6 @@ module Core
|
|
65
64
|
# @return [Array<Core::Models::Chatrooms::Messages>] all the messages ever sent by the user.
|
66
65
|
has_many :messages, class_name: 'Core::Models::Chatrooms::Message', inverse_of: :account
|
67
66
|
|
68
|
-
has_many :memberships, class_name: 'Core::Models::Chatrooms::Membership', inverse_of: :account
|
69
|
-
|
70
67
|
# @!attribute [rw] notifications
|
71
68
|
# @return [Array<Core::Models::Notification>] the notifications linked to this user.
|
72
69
|
embeds_many :notifications, class_name: 'Core::Models::Notification', inverse_of: :account
|
@@ -9,6 +9,8 @@ module Core
|
|
9
9
|
include Mongoid::Document
|
10
10
|
include Mongoid::Timestamps
|
11
11
|
|
12
|
+
store_in collection: 'sessions'
|
13
|
+
|
12
14
|
# @!attribute [rw] token
|
13
15
|
# @return [String] the unique token for this session, used to identify it and be sure the user is connected on this application.
|
14
16
|
field :token, type: String
|
data/lib/core/models/campaign.rb
CHANGED
@@ -6,6 +6,8 @@ module Core
|
|
6
6
|
include Mongoid::Document
|
7
7
|
include Mongoid::Timestamps
|
8
8
|
|
9
|
+
store_in collection: 'campaigns'
|
10
|
+
|
9
11
|
# @!attribute [rw] title
|
10
12
|
# @return [String] the title, or name, of the campaign, used to identify it in the list.
|
11
13
|
field :title, type: String
|
@@ -14,7 +16,7 @@ module Core
|
|
14
16
|
field :description, type: String
|
15
17
|
# @!attribute [rw] is_private
|
16
18
|
# @return [Boolean] TRUE if the campaign can be joined only by being invited by the creator, FALSE if it's publicly displayed and accessible.
|
17
|
-
field :is_private, type: Boolean, default: true
|
19
|
+
field :is_private, type: Mongoid::Boolean, default: true
|
18
20
|
# @!attribute [rw] tags
|
19
21
|
# @return [Array<String>] an array of tags describing characteristics of this campaign.
|
20
22
|
field :tags, type: Array, default: []
|
@@ -33,6 +35,10 @@ module Core
|
|
33
35
|
# @return [Core::Models::Chatrooms::Campaign] the chatroom linked to this campaign.
|
34
36
|
embeds_one :chatroom, class_name: 'Core::Models::Chatrooms::Campaign', inverse_of: :campaign
|
35
37
|
|
38
|
+
# @!attribute [rw] tokens
|
39
|
+
# @return [Array<Core::Models::Campaigns::Token>] the tokens declared in this campaign.
|
40
|
+
embeds_many :tokens, class_name: 'Core::Models::Campaigns::Token', inverse_of: :campaign
|
41
|
+
|
36
42
|
# @!attribute [rw] ruleset
|
37
43
|
# @return [Core::Models::Ruleset] the set of rules this campaign is based upon.
|
38
44
|
belongs_to :ruleset, class_name: 'Core::Models::Ruleset', inverse_of: :campaigns, optional: true
|
@@ -13,6 +13,8 @@ module Core
|
|
13
13
|
include Core::Models::Concerns::Enumerable
|
14
14
|
include Core::Models::Concerns::Historizable
|
15
15
|
|
16
|
+
store_in collection: 'invitations'
|
17
|
+
|
16
18
|
# @!attribute [rw] account
|
17
19
|
# @return [Core::Models::Account] the account the invitation has been issued to.
|
18
20
|
belongs_to :account, class_name: 'Core::Models::Account', inverse_of: :invitations
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Models
|
5
|
+
module Campaigns
|
6
|
+
# A map is a battleground where the players can place tokens and live the adventure.
|
7
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
|
+
class Map
|
9
|
+
include Mongoid::Document
|
10
|
+
include Mongoid::Timestamps
|
11
|
+
|
12
|
+
store_in collection: 'maps'
|
13
|
+
|
14
|
+
# @!attribute [rw] height
|
15
|
+
# @return [Integer] the number of lines in the map matric.
|
16
|
+
field :height, type: Integer, default: 1
|
17
|
+
# @!attribute [rw] width
|
18
|
+
# @return [Integer] the number of columns in the map matric.
|
19
|
+
field :width, type: Integer, default: 1
|
20
|
+
|
21
|
+
# @!attribute [rw] campaign
|
22
|
+
# @return [Core::Models::Campaign] the campaign in which the map can be found.
|
23
|
+
belongs_to :campaign, class_name: 'Core::Models::Campaign', inverse_of: :maps
|
24
|
+
|
25
|
+
# @!attribute [rw] positions
|
26
|
+
# @return [Array<Core::Model::Campaigns::TokenPosition>] the instanciated tokens on this map.
|
27
|
+
embeds_many :positions, class_name: 'Core::Models::Campaigns::TokenPosition', inverse_of: :map
|
28
|
+
|
29
|
+
validates :height,
|
30
|
+
numericality: { greater_than: 0, message: 'minimum' }
|
31
|
+
|
32
|
+
validates :width,
|
33
|
+
numericality: { greater_than: 0, message: 'minimum' }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|