Class: Mumukit::Auth::Metadata
- Inherits:
-
Object
- Object
- Mumukit::Auth::Metadata
- Defined in:
- lib/mumukit/auth/metadata.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_permission!(app, permission) ⇒ Object
- #admin?(slug) ⇒ Boolean
- #as_json(_options = {}) ⇒ Object
-
#initialize(json) ⇒ Metadata
constructor
A new instance of Metadata.
- #librarian?(slug) ⇒ Boolean
- #permissions(app) ⇒ Object
- #process_add_permission(app, permission) ⇒ Object
- #process_permission(new_permissions) ⇒ Object
- #process_remove_permission(app, permission) ⇒ Object
- #remove_permission!(app, permission) ⇒ Object
- #student?(slug) ⇒ Boolean
- #teacher?(slug) ⇒ Boolean
Constructor Details
#initialize(json) ⇒ Metadata
Returns a new instance of Metadata.
2 3 4 |
# File 'lib/mumukit/auth/metadata.rb', line 2 def initialize(json) @json = json end |
Class Method Details
.dump(metadata) ⇒ Object
61 62 63 |
# File 'lib/mumukit/auth/metadata.rb', line 61 def self.dump() .to_json end |
.load(json) ⇒ Object
57 58 59 |
# File 'lib/mumukit/auth/metadata.rb', line 57 def self.load(json) new(JSON.parse(json)) end |
Instance Method Details
#add_permission!(app, permission) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/mumukit/auth/metadata.rb', line 14 def (app, ) if (app).present? @json[app] = (app, ) else @json.merge!("#{app}" => {'permissions' => }) end end |
#admin?(slug) ⇒ Boolean
45 46 47 |
# File 'lib/mumukit/auth/metadata.rb', line 45 def admin?(slug) has_role? 'admin', slug end |
#as_json(_options = {}) ⇒ Object
6 7 8 |
# File 'lib/mumukit/auth/metadata.rb', line 6 def as_json(={}) @json end |
#librarian?(slug) ⇒ Boolean
41 42 43 |
# File 'lib/mumukit/auth/metadata.rb', line 41 def librarian?(slug) has_role? 'bibliotheca', slug end |
#permissions(app) ⇒ Object
10 11 12 |
# File 'lib/mumukit/auth/metadata.rb', line 10 def (app) @json.dig(app, 'permissions'). end |
#process_add_permission(app, permission) ⇒ Object
37 38 39 |
# File 'lib/mumukit/auth/metadata.rb', line 37 def (app, ) ((app).as_json + ":#{permission}") end |
#process_permission(new_permissions) ⇒ Object
29 30 31 |
# File 'lib/mumukit/auth/metadata.rb', line 29 def () {'permissions' => Mumukit::Auth::Permissions.load().to_s} end |
#process_remove_permission(app, permission) ⇒ Object
33 34 35 |
# File 'lib/mumukit/auth/metadata.rb', line 33 def (app, ) ((app).as_json.split(':').reject { |it| it == }.join(':')) end |
#remove_permission!(app, permission) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/mumukit/auth/metadata.rb', line 22 def (app, ) if (app).present? @json[app] = (app, ) end @json.delete(app) if @json.dig(app, 'permissions').blank? end |
#student?(slug) ⇒ Boolean
53 54 55 |
# File 'lib/mumukit/auth/metadata.rb', line 53 def student?(slug) has_role? 'atheneum', slug end |
#teacher?(slug) ⇒ Boolean
49 50 51 |
# File 'lib/mumukit/auth/metadata.rb', line 49 def teacher?(slug) has_role? 'classroom', slug end |