Class: Goldberg::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role_id) ⇒ Credentials

Create a new credentials object for the given role



10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 10

def initialize(role_id)
  @role_id = role_id

  role = Role.find(@role_id)
  @updated_at = role.updated_at

  roles = role.get_parents
  @role_ids = Array.new
  for r in roles do
    @role_ids << r.id
  end

  permissions = Permission.find_for_role(@role_ids)
  @permission_ids = Array.new
  for p in permissions do
    @permission_ids << p.id
  end

  if @permission_ids.length < 1
    @permission_ids << 0
  end

  actions = ControllerAction.actions_allowed(@permission_ids)
  @actions = Hash.new
  for a in actions do
    @actions[a.site_controller.name] ||= Hash.new
    if a.allowed.to_i == 1
      @actions[a.site_controller.name][a.name] = true
    else
      @actions[a.site_controller.name][a.name] = false
    end
  end

  sc = SiteController.table_name
  controllers = SiteController.find_by_sql ["select sc.*, (case when permission_id in (?) then 1 else 0 end) as allowed from #{sc} sc", @permission_ids]
  @controllers = Hash.new
  for c in controllers do
    if c.allowed.to_i == 1
      @controllers[c.name] = true
    else
      @controllers[c.name] = false
    end
  end

  cp = ContentPage.table_name
  pages = ContentPage.find_by_sql ["select id, name, permission_id, (case when permission_id in (?) then 1 else 0 end) as allowed from #{cp}", @permission_ids]
  @pages = Hash.new
  for p in pages do
    if p.allowed.to_i == 1
      @pages[p.name] = true
    else
      @pages[p.name] = false
    end
  end
  
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



6
7
8
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 6

def actions
  @actions
end

#controllersObject

Returns the value of attribute controllers.



6
7
8
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 6

def controllers
  @controllers
end

#pagesObject

Returns the value of attribute pages.



6
7
8
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 6

def pages
  @pages
end

#permission_idsObject

Returns the value of attribute permission_ids.



5
6
7
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 5

def permission_ids
  @permission_ids
end

#role_idObject

Returns the value of attribute role_id.



4
5
6
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 4

def role_id
  @role_id
end

#role_idsObject

Returns the value of attribute role_ids.



4
5
6
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 4

def role_ids
  @role_ids
end

#updated_atObject

Returns the value of attribute updated_at.



4
5
6
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 4

def updated_at
  @updated_at
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 7

def user
  @user
end

Instance Method Details

#action_authorised?(controller, action) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 81

def action_authorised?(controller, action)
  authorised = false  # default
  check_controller = false

  # Check if there's a specific permission for an action
  if @actions.has_key?(controller)
    if @actions[controller].has_key?(action)
      if @actions[controller][action]
        # logger.info "Action: authorised"
        authorised = true
      else
        # logger.info "Action: NOT authorised"
      end
    else
      check_controller = true
    end
  else
    check_controller = true
  end
  
  # Check if there's a general permission for a controller
  if check_controller
    authorised = controller_authorised?(controller)
  end
  
  # logger.info "Authorised? #{authorised.to_s}"
  return authorised
end

#controller_authorised?(controller) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 67

def controller_authorised?(controller)
  authorised = false  # default
  if @controllers.has_key?(controller)
    if @controllers[controller]
      # logger.info "Controller: authorised"
      authorised = true
    else
      # logger.info "Controller: NOT authorised"
    end
  else
  end
  return authorised
end

#page_authorised?(page) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/six-updater-web/vendor/plugins/goldberg/app/models/goldberg/credentials.rb', line 110

def page_authorised?(page)
  authorised = false  # default
  
  if page and @pages.has_key?(page.to_s)
    if @pages[page.to_s] == true
      # logger.info "Page: authorised"
      authorised = true
    else
    # logger.info "Page: NOT authorised"
    end
  else
    # logger.warn "(Unknown page? #{page})"
  end
  
  return authorised
end