Class Role
In: lib/role.rb
Parent: ActiveRecord::Base

Methods

Public Class methods

Actions for Permissions.

[Source]

    # File lib/role.rb, line 13
13:   def self.actions
14:     %w{
15:       ASSIGN_MEMBERS
16:       DEFINE_PERMISSIONS
17:       DELETE
18:       MANAGE_ANNOUNCEMENTS
19:       PERMISSIONS
20:       UPDATE
21:       VIEW
22:     }
23:   end

Creates a new role.

This process is engineered by creating a new role with Liferay‘s (v. 5.1.1) tools and inspecting the database dump diffs.

Mandatory parameters:

 - companyid
 - name

[Source]

     # File lib/role.rb, line 33
 33:   def initialize(params)
 34:     raise 'No companyid given' unless (params[:companyid] or params[:company])
 35:     raise 'No name given' unless params[:name]
 36: 
 37:     super(params)
 38: 
 39:     # COPY role_ (roleid, companyid, classnameid, classpk, name, description, type_) FROM stdin;
 40:     # +10151    10109      0    0  Regular role     This role is a test 1
 41: 
 42:     self.classnameid ||= 0
 43:     self.classpk     ||= 0
 44:     self.description ||= ''
 45:     # Type: 1 = regular, 2 = community, 3 = organization
 46:     self.type_       ||= 1
 47: 
 48:     self.save
 49: 
 50:     # Resource with code scope 1 is primkey'd to company.
 51:     # Resource with code scope 4 is primkey'd to this role.
 52: 
 53:     # These are created regardless of what type_ is.
 54: 
 55:     # COPY resourcecode (codeid, companyid, name, scope) FROM stdin;
 56:     # +29       10109 com.liferay.portal.model.Role   1
 57:     # +30       10109 com.liferay.portal.model.Role   4
 58: 
 59:     [1,4].each do |scope|
 60:       rc = self.resource_code(scope)
 61:       unless rc
 62:         ResourceCode.create(
 63:           :companyid => self.companyid,
 64:           :name      => self.liferay_class,
 65:           :scope     => scope
 66:         )
 67:       end
 68:     end
 69: 
 70:     # COPY resource_ (resourceid, codeid, primkey) FROM stdin;
 71:     # +33       29    10109
 72:     # +34       30    10151
 73: 
 74:     rc = self.resource_code(1)
 75:     raise 'Required ResourceCode not found' unless rc
 76:     r = Resource.find(:first, :conditions => "codeid=#{rc.id} AND primkey='#{self.companyid}'")
 77:     unless r
 78:       Resource.create(
 79:         :codeid  => rc.id,
 80:         :primkey => self.companyid
 81:       )
 82:     end
 83: 
 84:     rc = self.resource_code(4)
 85:     raise 'Required ResourceCode not found' unless rc
 86:     r = Resource.create(
 87:       :codeid  => rc.id,
 88:       :primkey => self.id
 89:     )
 90: 
 91:     # Permissions (given to administrators)
 92: 
 93:     #  COPY permission_ (permissionid, companyid, actionid, resourceid) FROM stdin;
 94:     # +70     10109   ASSIGN_MEMBERS  34
 95:     # +71     10109   DEFINE_PERMISSIONS      34
 96:     # +72     10109   DELETE  34
 97:     # +73     10109   MANAGE_ANNOUNCEMENTS    34
 98:     # +74     10109   PERMISSIONS     34
 99:     # +75     10109   UPDATE  34
100:     # +76     10109   VIEW    34
101: 
102:     # COPY users_permissions (userid, permissionid) FROM stdin;
103:     # +10129    70
104:     # +10129    71
105:     # +10129    72
106:     # +10129    73
107:     # +10129    74
108:     # +10129    75
109:     # +10129    76
110: 
111:     self.class.actions.each do |actionid|
112:       p = Permission.create(
113:         :companyid  => self.companyid,
114:         :actionid   => actionid,
115:         :resourceid => r.id
116:       )
117:       self.company.administrators.each do |user|
118:         user.user_permissions << p
119:       end
120:     end
121:   end

Public Instance methods

[Source]

     # File lib/role.rb, line 123
123:   def destroy_without_callbacks
124:     unless new_record?
125:       rc = self.resource_code(4)
126:       if rc
127:         r = Resource.find(:first, :conditions => "codeid=#{rc.id} AND primkey='#{self.id}'")
128:         if r
129:           self.class.actions.each do |actionid|
130:             p = Permission.find(:first,
131:               :conditions => "companyid=#{self.companyid} AND actionid='#{actionid}' AND resourceid=#{r.id}")
132:             next unless p
133:             p.users.each do |user|
134:               user.user_permissions.delete(p)
135:             end
136:             p.groups.each do |group|
137:               group.permissions.delete(p)
138:             end
139:             p.destroy
140:           end
141:           r.destroy
142:         end
143:         rc.destroy
144:       end
145: 
146:       self.users.each do |user|
147:         user.roles.delete(self)
148:       end
149: 
150:       self.groups.each do |group|
151:         group.roles.delete(self)
152:       end
153: 
154:       super
155: 
156:     end
157:     freeze
158:   end

com.liferay.portal.model.Role

[Source]

    # File lib/role.rb, line 8
 8:   def liferay_class
 9:     'com.liferay.portal.model.Role'
10:   end

ResourceCode associated to this instance (and scope)

[Source]

     # File lib/role.rb, line 183
183:   def resource_code(scope=4)
184:     ResourceCode.find(:first,
185:       :conditions => "companyid=#{self.companyid} AND name='#{self.liferay_class}' AND scope=#{scope}")
186:   end

[Validate]