Class: SakaiInfo::AuthzRole

Inherits:
SakaiObject show all
Defined in:
lib/sakai-info/authz.rb

Constant Summary collapse

@@cache =
{}

Instance Attribute Summary collapse

Attributes inherited from SakaiObject

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SakaiObject

all_serializations, #dbrow_only_serialization, #dbrow_serialization, #default_serialization, #object_type_serialization, #serialize, #to_json, #to_yaml

Constructor Details

#initialize(id, name) ⇒ AuthzRole

Returns a new instance of AuthzRole.



16
17
18
19
# File 'lib/sakai-info/authz.rb', line 16

def initialize(id, name)
  @id = id
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/sakai-info/authz.rb', line 14

def name
  @name
end

Class Method Details

.find(id_or_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sakai-info/authz.rb', line 57

def self.find(id_or_name)
  id_or_name = id_or_name.to_s
  role = nil
  begin
    role = AuthzRole.find_by_name(id_or_name)
  rescue ObjectNotFoundException
    # just in case
    role = AuthzRole.find_by_id(id_or_name)
  end
  if role.nil?
    raise ObjectNotFoundException.new(AuthzRole, id_or_name)
  end
  role
end

.find_by_id(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sakai-info/authz.rb', line 26

def self.find_by_id(id)
  id = id.to_s
  if @@cache[id].nil?
    row = DB.connect.fetch("select role_name from sakai_realm_role " +
                           "where role_key = ?", id.to_i).first
    if row.nil?
      raise ObjectNotFoundException.new(AuthzRole, id)
    end
    @@cache[id] = AuthzRole.new(id, row[:role_name])
    @@cache[name] = @@cache[id]
  end
  @@cache[id]
end

.find_by_name(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sakai-info/authz.rb', line 40

def self.find_by_name(name)
  if name.nil?
    raise ObjectNotFoundException.new(AuthzRole, "")
  end
  if @@cache[name].nil?
    row = DB.connect.fetch("select role_key from sakai_realm_role " +
                           "where role_name = ?", name).first
    if row.nil?
      raise ObjectNotFoundException.new(AuthzRole, name)
    end
    id = row[:role_key].to_i.to_s
    @@cache[name] = AuthzRole.new(id, name)
    @@cache[id] = @@cache[name]
  end
  @@cache[name]
end

.find_by_realm_id_and_function_id(realm_id, function_id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sakai-info/authz.rb', line 72

def self.find_by_realm_id_and_function_id(realm_id, function_id)
  roles = []
  DB.connect.fetch("select role_key, role_name from " +
                   "sakai_realm_role where role_key in " +
                   "(select role_key from sakai_realm_rl_fn " +
                   "where realm_key=? and function_key=?) " +
                   "order by role_name", realm_id, function_id) do |row|
    role_id = row[:role_key].to_i.to_s
    role_name = row[:role_name]
    roles << AuthzRole.new(role_id, role_name)
  end
  roles
end

Instance Method Details

#to_sObject



21
22
23
# File 'lib/sakai-info/authz.rb', line 21

def to_s
  name
end