Class: Yus::Privilege

Inherits:
Object
  • Object
show all
Defined in:
lib/yus/privilege.rb,
lib/yus/persistence/og.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrivilege

Returns a new instance of Privilege.



7
8
9
# File 'lib/yus/privilege.rb', line 7

def initialize
  @items = {}
end

Instance Attribute Details

#expiry_time(item = :everything) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/yus/privilege.rb', line 10

def expiry_time(item=:everything)
  if(time = [@items[item], @items[:everything]].compact.max)
    time if time.is_a?(Time)
  else
    raise NotPrivilegedError
  end
end

Instance Method Details

#grant(item, expiry_time = :never) ⇒ Object



17
18
19
# File 'lib/yus/privilege.rb', line 17

def grant(item, expiry_time=:never)
  @items.store(item, expiry_time)
end

#granted?(item) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yus/privilege.rb', line 20

def granted?(item)
  if(expiry_time = @items[item])
    case expiry_time
    when Time
      Time.now < expiry_time
    else
      true
    end
  elsif(@items.include?(:everything))
    # check time
    granted?(:everything)
  else
    item = item.to_s.dup
    if(item[-1] != ?*)
      while(!item.empty?)
        item.slice!(/[^.]*$/)
        if(granted?(item + "*"))
          return true
        end
        item.chop!
      end
    end
    false
  end
end

#infoObject



45
46
47
48
49
50
51
52
53
# File 'lib/yus/privilege.rb', line 45

def info
  @items.collect { |item, time|
    info = [item.to_s]
    if time.is_a?(Time)
      info.push time
    end
    info
  }.sort
end

#revoke(item, expiry_time = nil) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/yus/privilege.rb', line 54

def revoke(item, expiry_time=nil)
  case expiry_time
  when Time
    @items.store(item, expiry_time)
  else
    @items.delete(item)
  end
end