Class: Yus::Entity
- Inherits:
-
Object
- Object
- Yus::Entity
- Includes:
- ODBA::Persistable
- Defined in:
- lib/yus/entity.rb,
lib/yus/persistence/og.rb,
lib/yus/persistence/odba.rb
Constant Summary collapse
- ODBA_SERIALIZABLE =
['@last_logins', '@privileges', '@preferences', '@tokens']
Instance Attribute Summary collapse
-
#affiliations ⇒ Object
readonly
Returns the value of attribute affiliations.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#passhash ⇒ Object
writeonly
Sets the attribute passhash.
-
#privileges ⇒ Object
readonly
Returns the value of attribute privileges.
-
#valid_from ⇒ Object
Returns the value of attribute valid_from.
-
#valid_until ⇒ Object
Returns the value of attribute valid_until.
Class Method Summary collapse
Instance Method Summary collapse
- #allowed?(action, item = :everything) ⇒ Boolean
- #authenticate(passhash) ⇒ Object
- #authenticate_token(token) ⇒ Object
- #detect_circular_affiliation(entity) ⇒ Object
- #get_preference(key, domain = 'global') ⇒ Object
- #grant(action, item = :everything, expires = :never) ⇒ Object
- #info(recursive = false) ⇒ Object
-
#initialize(name, valid_until = nil, valid_from = Time.now) ⇒ Entity
constructor
A new instance of Entity.
- #join(entity) ⇒ Object
- #last_login(domain) ⇒ Object
- #leave(entity) ⇒ Object
- #login(domain) ⇒ Object
- #odba_join ⇒ Object
- #odba_leave ⇒ Object
- #privileged?(action, item = :everything) ⇒ Boolean
- #privileged_until(action, item = :everything) ⇒ Object
- #remove_token(token) ⇒ Object
- #rename(new_name) ⇒ Object
- #revoke(action, item = :everything, time = nil) ⇒ Object
- #set_preference(key, value, domain = nil) ⇒ Object
- #set_token(token, expires) ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, valid_until = nil, valid_from = Time.now) ⇒ Entity
Returns a new instance of Entity.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/yus/entity.rb', line 17 def initialize(name, valid_until=nil, valid_from=Time.now) @name = name.to_s @valid_until = valid_until @valid_from = valid_from @affiliations = [] @privileges = Hash.new(false) @preferences = {} @last_logins = {} @tokens = {} end |
Instance Attribute Details
#affiliations ⇒ Object (readonly)
Returns the value of attribute affiliations.
14 15 16 |
# File 'lib/yus/entity.rb', line 14 def affiliations @affiliations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/yus/entity.rb', line 14 def name @name end |
#passhash=(value) ⇒ Object (writeonly)
Sets the attribute passhash
16 17 18 |
# File 'lib/yus/entity.rb', line 16 def passhash=(value) @passhash = value end |
#privileges ⇒ Object (readonly)
Returns the value of attribute privileges.
14 15 16 |
# File 'lib/yus/entity.rb', line 14 def privileges @privileges end |
#valid_from ⇒ Object
Returns the value of attribute valid_from.
15 16 17 |
# File 'lib/yus/entity.rb', line 15 def valid_from @valid_from end |
#valid_until ⇒ Object
Returns the value of attribute valid_until.
15 16 17 |
# File 'lib/yus/entity.rb', line 15 def valid_until @valid_until end |
Class Method Details
.sanitize(term) ⇒ Object
121 122 123 |
# File 'lib/yus/entity.rb', line 121 def Entity.sanitize(term) term.to_s.downcase end |
Instance Method Details
#allowed?(action, item = :everything) ⇒ Boolean
27 28 29 30 |
# File 'lib/yus/entity.rb', line 27 def allowed?(action, item=:everything) valid? && privileged?(action, item) \ || @affiliations.any? { |entity| entity.allowed?(action, item) } end |
#authenticate(passhash) ⇒ Object
31 32 33 |
# File 'lib/yus/entity.rb', line 31 def authenticate(passhash) @passhash == passhash.to_s end |
#authenticate_token(token) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/yus/entity.rb', line 34 def authenticate_token(token) if expires = (@tokens ||= {}).delete(token) expires > Time.now else # Hacking-Attempt? Remove all Tokens for this user. @tokens.clear false end end |
#detect_circular_affiliation(entity) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/yus/entity.rb', line 43 def detect_circular_affiliation(entity) _detect_circular_affiliation(entity) rescue CircularAffiliationError => error error. << ' <- "' << entity.name << '"' raise end |
#get_preference(key, domain = 'global') ⇒ Object
82 83 84 |
# File 'lib/yus/entity.rb', line 82 def get_preference(key, domain='global') domain_preferences(domain)[key] || domain_preferences('global')[key] end |
#grant(action, item = :everything, expires = :never) ⇒ Object
49 50 51 52 |
# File 'lib/yus/entity.rb', line 49 def grant(action, item=:everything, expires=:never) action = Entity.sanitize(action) (@privileges[action] ||= Privilege.new).grant(item, expires) end |
#info(recursive = false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/yus/entity.rb', line 53 def info(recursive=false) info = [ @name ] @privileges.sort.each { |action, priv| info.push [action, priv.info] } if recursive @affiliations.sort_by { |entity| entity.name }.each { |entity| info.push entity.info(true) } elsif !@affiliations.empty? info.push @affiliations.collect { |entity| entity.name }.sort end info end |
#join(entity) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/yus/entity.rb', line 67 def join(party) unless(@affiliations.include?(party)) party.detect_circular_affiliation(self) @affiliations.push(party) end end |
#last_login(domain) ⇒ Object
73 74 75 |
# File 'lib/yus/entity.rb', line 73 def last_login(domain) (@last_logins ||= {})[domain] end |
#leave(entity) ⇒ Object
76 77 78 |
# File 'lib/yus/entity.rb', line 76 def leave(party) @affiliations.delete(party) end |
#login(domain) ⇒ Object
79 80 81 |
# File 'lib/yus/entity.rb', line 79 def login(domain) (@last_logins ||= {}).store(domain, Time.now) end |
#odba_join ⇒ Object
50 51 52 53 54 55 |
# File 'lib/yus/persistence/odba.rb', line 50 def join(party) unless(@affiliations.include?(party)) party.detect_circular_affiliation(self) @affiliations.push(party) end end |
#odba_leave ⇒ Object
51 52 53 |
# File 'lib/yus/persistence/odba.rb', line 51 def leave(party) @affiliations.delete(party) end |
#privileged?(action, item = :everything) ⇒ Boolean
85 86 87 88 |
# File 'lib/yus/entity.rb', line 85 def privileged?(action, item=:everything) (privilege = @privileges[Entity.sanitize(action)]) \ && privilege.granted?(item) end |
#privileged_until(action, item = :everything) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/yus/entity.rb', line 89 def privileged_until(action, item=:everything) if(privilege = @privileges[Entity.sanitize(action)]) privilege.expiry_time(item) else raise NotPrivilegedError end end |
#remove_token(token) ⇒ Object
96 97 98 |
# File 'lib/yus/entity.rb', line 96 def remove_token(token) @tokens.delete(token) if @tokens end |
#rename(new_name) ⇒ Object
99 100 101 |
# File 'lib/yus/entity.rb', line 99 def rename(new_name) @name = new_name end |
#revoke(action, item = :everything, time = nil) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/yus/entity.rb', line 102 def revoke(action, item=:everything, time=nil) action = Entity.sanitize(action) if(priv = @privileges[action]) priv.revoke(item, time) end end |
#set_preference(key, value, domain = nil) ⇒ Object
108 109 110 |
# File 'lib/yus/entity.rb', line 108 def set_preference(key, value, domain=nil) domain_preferences(domain || 'global')[key] = value end |
#set_token(token, expires) ⇒ Object
111 112 113 |
# File 'lib/yus/entity.rb', line 111 def set_token(token, expires) (@tokens ||= {}).store token, expires end |
#to_s ⇒ Object
114 115 116 |
# File 'lib/yus/entity.rb', line 114 def to_s @name end |
#valid? ⇒ Boolean
117 118 119 120 |
# File 'lib/yus/entity.rb', line 117 def valid? now = Time.now @valid_from < now && (!@valid_until || @valid_until > now) end |