Class: Gemstash::Authorization
- Inherits:
-
Object
- Object
- Gemstash::Authorization
show all
- Extended by:
- Env::Helper, Logging
- Defined in:
- lib/gemstash/authorization.rb
Overview
Authorization mechanism to manipulate private gems.
Constant Summary
collapse
- VALID_PERMISSIONS =
%w[push yank fetch].freeze
Constants included
from Logging
Logging::LEVELS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
log, log_error, logger, reset, setup_logger
Constructor Details
Returns a new instance of Authorization.
65
66
67
68
69
70
|
# File 'lib/gemstash/authorization.rb', line 65
def initialize(record)
@auth_key = record.auth_key
@name = record.name
@all = record.permissions == "all"
@permissions = Set.new(record.permissions.split(","))
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
16
17
18
|
# File 'lib/gemstash/authorization.rb', line 16
def name
@name
end
|
Class Method Details
.[](auth_key) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/gemstash/authorization.rb', line 52
def self.[](auth_key)
cached_auth = gemstash_env.cache.authorization(auth_key)
return cached_auth if cached_auth
record = Gemstash::DB::Authorization[auth_key: auth_key]
if record
auth = new(record)
gemstash_env.cache.set_authorization(record.auth_key, auth)
auth
end
end
|
.authorize(auth_key, permissions, name = nil) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/gemstash/authorization.rb', line 18
def self.authorize(auth_key, permissions, name = nil)
raise "Authorization key is required!" if auth_key.to_s.strip.empty?
raise "Permissions are required!" if permissions.to_s.empty?
unless permissions == "all"
permissions.each do |permission|
raise "Invalid permission '#{permission}'" unless VALID_PERMISSIONS.include?(permission)
end
permissions = permissions.join(",")
end
Gemstash::DB::Authorization.insert_or_update(auth_key, permissions, name)
gemstash_env.cache.invalidate_authorization(auth_key)
log.info "Authorization '#{auth_key}' updated with access to '#{permissions}'"
end
|
.check(auth_key, permission) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/gemstash/authorization.rb', line 44
def self.check(auth_key, permission)
raise NotAuthorizedError, "Authorization key required" if auth_key.to_s.strip.empty?
auth = self[auth_key]
raise NotAuthorizedError, "Authorization key is invalid" unless auth
raise NotAuthorizedError, "Authorization key doesn't have #{permission} access" unless auth.can?(permission)
end
|
.remove(auth_key) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/gemstash/authorization.rb', line 35
def self.remove(auth_key)
record = Gemstash::DB::Authorization[auth_key: auth_key]
return unless record
record.destroy
gemstash_env.cache.invalidate_authorization(auth_key)
log.info "Authorization '#{auth_key}' with access to '#{record.permissions}' removed"
end
|
Instance Method Details
#all? ⇒ Boolean
78
79
80
|
# File 'lib/gemstash/authorization.rb', line 78
def all?
@all
end
|
#can?(permission) ⇒ Boolean
72
73
74
75
76
|
# File 'lib/gemstash/authorization.rb', line 72
def can?(permission)
raise "Invalid permission '#{permission}'" unless VALID_PERMISSIONS.include?(permission)
all? || @permissions.include?(permission)
end
|
#fetch? ⇒ Boolean
90
91
92
|
# File 'lib/gemstash/authorization.rb', line 90
def fetch?
can?("fetch")
end
|
#push? ⇒ Boolean
82
83
84
|
# File 'lib/gemstash/authorization.rb', line 82
def push?
can?("push")
end
|
#yank? ⇒ Boolean
86
87
88
|
# File 'lib/gemstash/authorization.rb', line 86
def yank?
can?("yank")
end
|