class Yus::Entity
Constants
- ODBA_SERIALIZABLE
Attributes
affiliations[R]
name[R]
passhash[W]
privileges[R]
valid_from[RW]
valid_until[RW]
Public Class Methods
new(name, valid_until=nil, valid_from=Time.now)
click to toggle source
# 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
sanitize(term)
click to toggle source
# File lib/yus/entity.rb, line 121 def Entity.sanitize(term) term.to_s.downcase end
Public Instance Methods
allowed?(action, item=:everything)
click to toggle source
# 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)
click to toggle source
# File lib/yus/entity.rb, line 31 def authenticate(passhash) @passhash == passhash.to_s end
authenticate_token(token)
click to toggle source
# 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)
click to toggle source
# File lib/yus/entity.rb, line 43 def detect_circular_affiliation(entity) _detect_circular_affiliation(entity) rescue CircularAffiliationError => error error.message << ' <- "' << entity.name << '"' raise end
get_preference(key, domain='global')
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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(party)
click to toggle source
# File lib/yus/entity.rb, line 67 def join(party) unless(@affiliations.include?(party)) party.detect_circular_affiliation(self) @affiliations.push(party) end end
Also aliased as: odba_join
last_login(domain)
click to toggle source
# File lib/yus/entity.rb, line 73 def last_login(domain) (@last_logins ||= {})[domain] end
leave(party)
click to toggle source
# File lib/yus/entity.rb, line 76 def leave(party) @affiliations.delete(party) end
Also aliased as: odba_leave
login(domain)
click to toggle source
# File lib/yus/entity.rb, line 79 def login(domain) (@last_logins ||= {}).store(domain, Time.now) end
privileged?(action, item=:everything)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/yus/entity.rb, line 96 def remove_token(token) @tokens.delete(token) if @tokens end
rename(new_name)
click to toggle source
# File lib/yus/entity.rb, line 99 def rename(new_name) @name = new_name end
revoke(action, item=:everything, time=nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/yus/entity.rb, line 111 def set_token(token, expires) (@tokens ||= {}).store token, expires end
to_s()
click to toggle source
# File lib/yus/entity.rb, line 114 def to_s @name end
valid?()
click to toggle source
# File lib/yus/entity.rb, line 117 def valid? now = Time.now @valid_from < now && (!@valid_until || @valid_until > now) end
Protected Instance Methods
_detect_circular_affiliation(entity)
click to toggle source
# File lib/yus/entity.rb, line 125 def _detect_circular_affiliation(entity) if(@affiliations.include?(entity)) raise CircularAffiliationError, "circular affiliation detected: \"#{entity.name}\"" end @affiliations.each { |aff| aff._detect_circular_affiliation(entity) } rescue CircularAffiliationError => error error.message << ' <- "' << @name << '"' raise end
Private Instance Methods
domain_preferences(domain)
click to toggle source
# File lib/yus/entity.rb, line 136 def domain_preferences(domain) @preferences[domain] ||= {} end