class Granted::Granter

Public Class Methods

new() click to toggle source
# File lib/granted/granter.rb, line 3
def initialize
end

Public Instance Methods

from(grantee) click to toggle source
# File lib/granted/granter.rb, line 10
def from(grantee)
  accept(grantee: grantee)
end
grant() click to toggle source
# File lib/granted/granter.rb, line 26
def grant
  accept(action: :grant)
end
on(subject) click to toggle source
# File lib/granted/granter.rb, line 14
def on(subject)
  accept(subject: subject)
end
revoke() click to toggle source
# File lib/granted/granter.rb, line 30
def revoke
  accept(action: :revoke)
end
right(right) click to toggle source
# File lib/granted/granter.rb, line 22
def right(right)
  rights(right)
end
rights(*rights) click to toggle source
# File lib/granted/granter.rb, line 18
def rights(*rights)
  accept(rights: [rights].flatten)
end
to(grantee) click to toggle source
# File lib/granted/granter.rb, line 6
def to(grantee)
  accept(grantee: grantee)
end

Private Instance Methods

accept(options) click to toggle source
# File lib/granted/granter.rb, line 36
def accept(options)
  @action  ||= options[:action]
  @grantee ||= options[:grantee]
  @subject ||= options[:subject]
  @rights  ||= options[:rights]
  finalize
end
finalize() click to toggle source
# File lib/granted/granter.rb, line 44
def finalize
  return self unless @grantee and @subject and @rights and @action
  @rights.map do |right|
    clazz = GrantClassFactory.get(right)
    selector = clazz.grantee(@grantee).subject(@subject)
    case @action.to_sym
    when :grant  then give_grant(selector)
    when :revoke then revoke_grant(selector)
    else raise "Invalid action @action"
    end
  end
end
give_grant(selector) click to toggle source
# File lib/granted/granter.rb, line 62
def give_grant(selector)
  selector.first_or_create
end
revoke_grant(selector) click to toggle source
# File lib/granted/granter.rb, line 57
def revoke_grant(selector)
  return true unless g = selector.first
  g.destroy
end