1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.convert;
16  
17  import com.liferay.counter.service.CounterLocalServiceUtil;
18  import com.liferay.portal.NoSuchResourceActionException;
19  import com.liferay.portal.convert.util.PermissionView;
20  import com.liferay.portal.convert.util.ResourcePermissionView;
21  import com.liferay.portal.kernel.dao.db.DB;
22  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
23  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
26  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.PropsKeys;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Tuple;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.model.Company;
37  import com.liferay.portal.model.Group;
38  import com.liferay.portal.model.ResourceAction;
39  import com.liferay.portal.model.ResourceCode;
40  import com.liferay.portal.model.ResourceConstants;
41  import com.liferay.portal.model.ResourcePermission;
42  import com.liferay.portal.model.Role;
43  import com.liferay.portal.model.RoleConstants;
44  import com.liferay.portal.model.impl.PermissionModelImpl;
45  import com.liferay.portal.model.impl.ResourceCodeModelImpl;
46  import com.liferay.portal.model.impl.ResourceModelImpl;
47  import com.liferay.portal.model.impl.ResourcePermissionModelImpl;
48  import com.liferay.portal.model.impl.RoleModelImpl;
49  import com.liferay.portal.security.permission.PermissionCacheUtil;
50  import com.liferay.portal.security.permission.ResourceActionsUtil;
51  import com.liferay.portal.service.ClassNameLocalServiceUtil;
52  import com.liferay.portal.service.CompanyLocalServiceUtil;
53  import com.liferay.portal.service.GroupLocalServiceUtil;
54  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
55  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
56  import com.liferay.portal.service.RoleLocalServiceUtil;
57  import com.liferay.portal.service.UserLocalServiceUtil;
58  import com.liferay.portal.service.persistence.BatchSessionUtil;
59  import com.liferay.portal.upgrade.util.Table;
60  import com.liferay.portal.util.MaintenanceUtil;
61  import com.liferay.portal.util.PropsValues;
62  import com.liferay.portal.util.ShutdownUtil;
63  
64  import java.io.FileReader;
65  import java.io.FileWriter;
66  import java.io.Writer;
67  
68  import java.sql.Connection;
69  import java.sql.PreparedStatement;
70  import java.sql.ResultSet;
71  import java.sql.Types;
72  
73  import java.util.ArrayList;
74  import java.util.Collections;
75  import java.util.HashMap;
76  import java.util.HashSet;
77  import java.util.List;
78  import java.util.Map;
79  import java.util.Set;
80  
81  import org.apache.commons.collections.map.MultiValueMap;
82  
83  /**
84   * <a href="ConvertPermissionAlgorithm.java.html"><b><i>View Source</i></b></a>
85   *
86   * <p>
87   * This class converts all existing permissions from the legacy permissions
88   * algorithm to the latest algorithm.
89   * </p>
90   *
91   * @author Alexander Chow
92   */
93  public class ConvertPermissionAlgorithm extends ConvertProcess {
94  
95      public String getDescription() {
96          return "convert-legacy-permission-algorithm";
97      }
98  
99      public String[] getParameterNames() {
100         return new String[] {
101             "generate-custom-roles=checkbox"
102         };
103     }
104 
105     public boolean isEnabled() {
106         boolean enabled = false;
107 
108         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM < 6) {
109             enabled = true;
110         }
111 
112         return enabled;
113     }
114 
115     protected void convertToBitwise() throws Exception {
116 
117         // ResourceAction and ResourcePermission
118 
119         MaintenanceUtil.appendStatus(
120             "Generating ResourceAction and ResourcePermission data");
121 
122         Table table = new Table(
123             ResourceCodeModelImpl.TABLE_NAME,
124             new Object[][] {
125                 {"name", new Integer(Types.VARCHAR)}
126             });
127 
128         table.setSelectSQL(
129             "SELECT name FROM " + ResourceCodeModelImpl.TABLE_NAME +
130                 " GROUP BY name");
131 
132         String tempFile = table.generateTempFile();
133 
134         UnsyncBufferedReader resourceNameReader = new UnsyncBufferedReader(
135             new FileReader(tempFile));
136 
137         Writer resourcePermissionWriter = new UnsyncBufferedWriter(
138             new FileWriter(tempFile + _EXT_RESOURCE_PERMISSION));
139 
140         PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM = 6;
141 
142         try {
143             String line = null;
144 
145             while (Validator.isNotNull(line = resourceNameReader.readLine())) {
146                 String[] values = StringUtil.split(line);
147 
148                 if (values.length == 0) {
149                     continue;
150                 }
151 
152                 String name = values[0];
153 
154                 List<String> defaultActionIds =
155                     ResourceActionsUtil.getResourceActions(name);
156 
157                 ResourceActionLocalServiceUtil.checkResourceActions(
158                     name, defaultActionIds);
159 
160                 convertResourcePermission(resourcePermissionWriter, name);
161             }
162 
163             resourcePermissionWriter.close();
164 
165             MaintenanceUtil.appendStatus("Updating ResourcePermission table");
166 
167             Table resourcePermissionTable = new Table(
168                 ResourcePermissionModelImpl.TABLE_NAME,
169                 ResourcePermissionModelImpl.TABLE_COLUMNS);
170 
171             resourcePermissionTable.populateTable(
172                 tempFile + _EXT_RESOURCE_PERMISSION);
173         }
174         catch (Exception e) {
175             PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM = 5;
176 
177             throw e;
178         }
179         finally {
180             resourceNameReader.close();
181 
182             resourcePermissionWriter.close();
183 
184             FileUtil.delete(tempFile);
185             FileUtil.delete(tempFile + _EXT_RESOURCE_PERMISSION);
186         }
187 
188         // Clean up
189 
190         MaintenanceUtil.appendStatus("Cleaning up legacy tables");
191 
192         DB db = DBFactoryUtil.getDB();
193 
194         db.runSQL("DELETE FROM " + ResourceCodeModelImpl.TABLE_NAME);
195         db.runSQL("DELETE FROM " + PermissionModelImpl.TABLE_NAME);
196         db.runSQL("DELETE FROM " + ResourceModelImpl.TABLE_NAME);
197         db.runSQL("DELETE FROM Roles_Permissions");
198 
199         MaintenanceUtil.appendStatus("Converted to bitwise permission");
200     }
201 
202     protected void convertToRBAC() throws Exception {
203         initializeRBAC();
204 
205         // Groups_Permissions
206 
207         convertPermissions(
208             RoleConstants.TYPE_COMMUNITY, "Groups_Permissions",
209             new String[] {"groupId"}, "Groups_Roles",
210             new Object[][] {
211                 {"groupId", Types.BIGINT}, {"roleId", Types.BIGINT}
212             });
213 
214         // OrgGroupPermission
215 
216         convertPermissions(
217             RoleConstants.TYPE_ORGANIZATION, "OrgGroupPermission",
218             new String[] {"organizationId", "groupId"}, "OrgGroupRole",
219             new Object[][] {
220                 {"organizationId", Types.BIGINT}, {"groupId", Types.BIGINT},
221                 {"roleId", Types.BIGINT}
222             });
223 
224         // Users_Permissions
225 
226         convertPermissions(
227             RoleConstants.TYPE_REGULAR, "Users_Permissions",
228             new String[] {"userId"}, "Users_Roles",
229             new Object[][] {
230                 {"userId", Types.BIGINT}, {"roleId", Types.BIGINT}
231             });
232 
233         // Clean up
234 
235         PermissionCacheUtil.clearCache();
236 
237         PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM = 5;
238 
239         MaintenanceUtil.appendStatus("Converted to RBAC permission");
240     }
241 
242     protected String convertGuestUsers(String legacyFile) throws Exception {
243         UnsyncBufferedReader legacyFileReader = new UnsyncBufferedReader(
244             new FileReader(legacyFile));
245 
246         Writer legacyFileUpdatedWriter = new UnsyncBufferedWriter(
247             new FileWriter(legacyFile + _UPDATED));
248         Writer legacyFileExtRolesPermissionsWriter = new UnsyncBufferedWriter(
249             new FileWriter(legacyFile + _EXT_ROLES_PERMIMISSIONS));
250 
251         try {
252             String line = null;
253 
254             while (Validator.isNotNull(line = legacyFileReader.readLine())) {
255                 String[] values = StringUtil.split(line);
256 
257                 long companyId = PermissionView.getCompanyId(values);
258                 long permissionId = PermissionView.getPermissionId(values);
259                 int scope = PermissionView.getScopeId(values);
260                 long userId = PermissionView.getPrimaryKey(values);
261 
262                 if ((scope == ResourceConstants.SCOPE_INDIVIDUAL) &&
263                     (_guestUsersSet.contains(userId))) {
264 
265                     long roleId = _guestRolesMap.get(companyId).getRoleId();
266 
267                     String key = roleId + "_" + permissionId;
268 
269                     if (_rolesPermissions.contains(key)) {
270                         continue;
271                     }
272                     else {
273                         _rolesPermissions.add(key);
274                     }
275 
276                     legacyFileExtRolesPermissionsWriter.write(
277                         roleId + "," + permissionId + "\n");
278                 }
279                 else {
280                     legacyFileUpdatedWriter.write(line + "\n");
281                 }
282             }
283         }
284         finally {
285             legacyFileReader.close();
286 
287             legacyFileUpdatedWriter.close();
288             legacyFileExtRolesPermissionsWriter.close();
289         }
290 
291         Table table = new Table(
292             "Roles_Permissions",
293             new Object[][] {
294                 {"roleId", Types.BIGINT}, {"permissionId", Types.BIGINT}
295             });
296 
297         table.populateTable(legacyFile + _EXT_ROLES_PERMIMISSIONS);
298 
299         FileUtil.delete(legacyFile);
300         FileUtil.delete(legacyFile + _EXT_ROLES_PERMIMISSIONS);
301 
302         return legacyFile + _UPDATED;
303     }
304 
305     protected void convertPermissions(
306             int type, String legacyName, String[] primKeys, String newName,
307             Object[][] newColumns)
308         throws Exception {
309 
310         MaintenanceUtil.appendStatus("Processing " + legacyName);
311 
312         Table legacyTable = new PermissionView(legacyName, primKeys);
313 
314         String legacyFile = legacyTable.generateTempFile();
315 
316         if (legacyFile == null) {
317             return;
318         }
319 
320         if (type == RoleConstants.TYPE_REGULAR) {
321             legacyFile = convertGuestUsers(legacyFile);
322 
323             MaintenanceUtil.appendStatus(
324                 "Converted guest users to guest roles");
325         }
326 
327         convertRoles(legacyFile, type, newName, newColumns);
328 
329         MaintenanceUtil.appendStatus("Converted roles for " + legacyName);
330 
331         DB db = DBFactoryUtil.getDB();
332 
333         db.runSQL(legacyTable.getDeleteSQL());
334 
335         FileUtil.delete(legacyFile);
336     }
337 
338     protected void convertResourcePermission(Writer writer, String name)
339         throws Exception {
340 
341         ResourcePermissionView resourcePermissionView =
342             new ResourcePermissionView(name);
343 
344         UnsyncBufferedReader resourcePermissionReader = null;
345 
346         String resourcePermissionFile =
347             resourcePermissionView.generateTempFile();
348 
349         if (resourcePermissionFile == null) {
350             return;
351         }
352 
353         MultiValueMap mvp = new MultiValueMap();
354 
355         try {
356             resourcePermissionReader = new UnsyncBufferedReader(
357                 new FileReader(resourcePermissionFile));
358 
359             String line = null;
360 
361             while (Validator.isNotNull(
362                     line = resourcePermissionReader.readLine())) {
363 
364                 String[] values = StringUtil.split(line);
365 
366                 String actionId = ResourcePermissionView.getActionId(values);
367                 long companyId = ResourcePermissionView.getCompanyId(values);
368                 int scope = ResourcePermissionView.getScope(values);
369                 String primKey = ResourcePermissionView.getPrimaryKey(values);
370                 long roleId = ResourcePermissionView.getRoleId(values);
371 
372                 mvp.put(new Tuple(companyId, scope, primKey, roleId), actionId);
373             }
374         }
375         finally {
376             if (resourcePermissionReader != null) {
377                 resourcePermissionReader.close();
378             }
379 
380             FileUtil.delete(resourcePermissionFile);
381         }
382 
383         for (Tuple key : (Set<Tuple>)mvp.keySet()) {
384             long resourcePermissionId = CounterLocalServiceUtil.increment(
385                 ResourcePermission.class.getName());
386 
387             long companyId = (Long)key.getObject(0);
388             int scope = (Integer)key.getObject(1);
389             String primKey = (String)key.getObject(2);
390             long roleId = (Long)key.getObject(3);
391 
392             String[] actionIdArray =
393                 (String[])mvp.getCollection(key).toArray(new String[0]);
394 
395             long actionIds = 0;
396 
397             for (String actionId : actionIdArray) {
398                 try {
399                     ResourceAction resourceAction =
400                         ResourceActionLocalServiceUtil.getResourceAction(
401                             name, actionId);
402 
403                     actionIds |= resourceAction.getBitwiseValue();
404                 }
405                 catch (NoSuchResourceActionException nsrae) {
406                     if (_log.isWarnEnabled()) {
407                         String msg = nsrae.getMessage();
408 
409                         _log.warn("Could not find resource action " + msg);
410                     }
411                 }
412             }
413 
414             writer.append(resourcePermissionId + StringPool.COMMA);
415             writer.append(companyId + StringPool.COMMA);
416             writer.append(name + StringPool.COMMA);
417             writer.append(scope + StringPool.COMMA);
418             writer.append(primKey + StringPool.COMMA);
419             writer.append(roleId + StringPool.COMMA);
420             writer.append(actionIds + StringPool.COMMA + StringPool.NEW_LINE);
421         }
422     }
423 
424     protected void convertRoles(
425             String legacyFile, int type, String newName, Object[][] newColumns)
426         throws Exception {
427 
428         UnsyncBufferedReader legacyFileReader = new UnsyncBufferedReader(
429             new FileReader(legacyFile));
430 
431         Writer legacyFileExtRoleWriter = new UnsyncBufferedWriter(
432             new FileWriter(legacyFile + _EXT_ROLE));
433         Writer legacyFileExtRolesPermissionsWriter = new UnsyncBufferedWriter(
434             new FileWriter(legacyFile + _EXT_ROLES_PERMIMISSIONS));
435         Writer legacyFileExtOtherRolesWriter = new UnsyncBufferedWriter(
436             new FileWriter(legacyFile + _EXT_OTHER_ROLES));
437 
438         try {
439 
440             // Group by resource id
441 
442             MultiValueMap mvp = new MultiValueMap();
443 
444             String line = null;
445 
446             while (Validator.isNotNull(line = legacyFileReader.readLine())) {
447                 String[] values = StringUtil.split(line);
448 
449                 long resourceId = PermissionView.getResourceId(values);
450 
451                 mvp.put(resourceId, values);
452             }
453 
454             // Assign role for each grouping
455 
456             for (Long key : (Set<Long>)mvp.keySet()) {
457                 List<String[]> valuesList = new ArrayList<String[]>(
458                     mvp.getCollection(key));
459 
460                 String[] values = valuesList.get(0);
461 
462                 long companyId = PermissionView.getCompanyId(values);
463                 long groupId = PermissionView.getPrimaryKey(values);
464                 String name = PermissionView.getNameId(values);
465                 int scope = PermissionView.getScopeId(values);
466 
467                 // Group action ids and permission ids
468 
469                 List<String> actionsIds = new ArrayList<String>();
470                 List<Long> permissionIds = new ArrayList<Long>();
471 
472                 for (String[] curValues : valuesList) {
473                     String actionId = PermissionView.getActionId(curValues);
474                     long permissionId = PermissionView.getPermissionId(
475                         curValues);
476 
477                     actionsIds.add(actionId);
478                     permissionIds.add(permissionId);
479                 }
480 
481                 // Look for owner and system roles
482 
483                 if ((type != RoleConstants.TYPE_ORGANIZATION) &&
484                     (scope == ResourceConstants.SCOPE_INDIVIDUAL)) {
485 
486                     // Find default actions
487 
488                     List<String> defaultActions = null;
489 
490                     if (type == RoleConstants.TYPE_REGULAR) {
491                         defaultActions =
492                             ResourceActionsUtil.getResourceActions(name);
493                     }
494                     else {
495                         defaultActions =
496                             ResourceActionsUtil.
497                                 getResourceCommunityDefaultActions(name);
498                     }
499 
500                     // Resolve owner and system roles
501 
502                     Role defaultRole = null;
503 
504                     if (type == RoleConstants.TYPE_REGULAR) {
505                         Collections.sort(actionsIds);
506                         Collections.sort(defaultActions);
507 
508                         if (defaultActions.equals(actionsIds)) {
509                             defaultRole = _ownerRolesMap.get(companyId);
510                         }
511                     }
512                     else {
513                         if (defaultActions.containsAll(actionsIds)) {
514                             Role[] defaultRoles = _defaultRolesMap.get(
515                                 companyId);
516 
517                             Group group = _groupsMap.get(groupId);
518 
519                             if (group == null) {
520                                 continue;
521                             }
522 
523                             if (group.isCommunity()) {
524                                 defaultRole = defaultRoles[0];
525                             }
526                             else if (group.isOrganization()) {
527                                 defaultRole = defaultRoles[1];
528                             }
529                             else if (group.isUser() || group.isUserGroup()) {
530                                 defaultRole = defaultRoles[2];
531                             }
532                         }
533                     }
534 
535                     if (defaultRole != null) {
536                         long roleId = defaultRole.getRoleId();
537 
538                         for (Long permissionId : permissionIds) {
539                             String curKey = roleId + "_" + permissionId;
540 
541                             if (_rolesPermissions.contains(curKey)) {
542                                 continue;
543                             }
544                             else {
545                                 _rolesPermissions.add(curKey);
546                             }
547 
548                             legacyFileExtRolesPermissionsWriter.write(
549                                 roleId + "," + permissionId + ",\n");
550                         }
551 
552                         continue;
553                     }
554                 }
555 
556                 if (isGenerateCustomRoles()) {
557 
558                     // Role_
559 
560                     long roleId = CounterLocalServiceUtil.increment();
561 
562                     String roleName = StringUtil.upperCaseFirstLetter(
563                         RoleConstants.getTypeLabel(type));
564 
565                     roleName += " " + Long.toHexString(roleId);
566 
567                     String[] roleColumns = new String[] {
568                         String.valueOf(roleId), String.valueOf(companyId),
569                         String.valueOf(
570                             ClassNameLocalServiceUtil.getClassNameId(
571                                 Role.class)),
572                         String.valueOf(roleId), roleName, StringPool.BLANK,
573                         "Autogenerated role from portal upgrade",
574                         String.valueOf(type), "lfr-permission-algorithm-5"
575                     };
576 
577                     for (int i = 0; i < roleColumns.length; i++) {
578                         legacyFileExtRoleWriter.write(
579                             roleColumns[i] + StringPool.COMMA);
580 
581                         if (i == (roleColumns.length - 1)) {
582                             legacyFileExtRoleWriter.write(StringPool.NEW_LINE);
583                         }
584                     }
585 
586                     // Roles_Permissions
587 
588                     for (Long permissionId : permissionIds) {
589                         String curKey = roleId + "_" + permissionId;
590 
591                         if (_rolesPermissions.contains(curKey)) {
592                             continue;
593                         }
594                         else {
595                             _rolesPermissions.add(curKey);
596                         }
597 
598                         legacyFileExtRolesPermissionsWriter.write(
599                             roleId + "," + permissionId + ",\n");
600                     }
601 
602                     // Others_Roles
603 
604                     for (int i = 0; i < newColumns.length - 1; i++) {
605                         legacyFileExtOtherRolesWriter.write(
606                             values[i] + StringPool.COMMA);
607                     }
608 
609                     legacyFileExtOtherRolesWriter.write(roleId + ",\n");
610                 }
611             }
612         }
613         finally {
614             legacyFileReader.close();
615 
616             legacyFileExtRoleWriter.close();
617             legacyFileExtRolesPermissionsWriter.close();
618             legacyFileExtOtherRolesWriter.close();
619         }
620 
621         // Role_
622 
623         Table roleTable = new Table(
624             RoleModelImpl.TABLE_NAME, RoleModelImpl.TABLE_COLUMNS);
625 
626         roleTable.populateTable(legacyFile + _EXT_ROLE);
627 
628         // Roles_Permissions
629 
630         Table rolesPermissionsTable = new Table(
631             "Roles_Permissions",
632             new Object[][] {
633                 {"roleId", Types.BIGINT}, {"permissionId", Types.BIGINT}
634             });
635 
636         rolesPermissionsTable.populateTable(
637             legacyFile + _EXT_ROLES_PERMIMISSIONS);
638 
639         // Others_Roles
640 
641         Table othersRolesTable = new Table(newName, newColumns);
642 
643         othersRolesTable.populateTable(legacyFile + _EXT_OTHER_ROLES);
644 
645         // Clean up
646 
647         FileUtil.delete(legacyFile + _EXT_ROLE);
648         FileUtil.delete(legacyFile + _EXT_ROLES_PERMIMISSIONS);
649         FileUtil.delete(legacyFile + _EXT_OTHER_ROLES);
650     }
651 
652     protected void doConvert() throws Exception {
653         try {
654             BatchSessionUtil.setEnabled(true);
655 
656             initialize();
657 
658             if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM < 5) {
659                 convertToRBAC();
660             }
661 
662             convertToBitwise();
663 
664             MaintenanceUtil.appendStatus(
665                 "Please set " + PropsKeys.PERMISSIONS_USER_CHECK_ALGORITHM +
666                     " in your portal-ext.properties to 6 and restart server");
667         }
668         finally {
669             ShutdownUtil.shutdown(0);
670         }
671     }
672 
673     protected void initialize() throws Exception {
674 
675         // Resource actions for unknown portlets
676 
677         List<ResourceCode> resourceCodes =
678             ResourceCodeLocalServiceUtil.getResourceCodes(
679                 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
680 
681         for (ResourceCode resourceCode : resourceCodes) {
682             String name = resourceCode.getName();
683 
684             if (!name.contains(StringPool.PERIOD)) {
685                 ResourceActionsUtil.getPortletResourceActions(name);
686             }
687         }
688     }
689 
690     protected void initializeRBAC() throws Exception {
691 
692         // System roles and default users
693 
694         List<Company> companies = CompanyLocalServiceUtil.getCompanies();
695 
696         for (Company company : companies) {
697             long companyId = company.getCompanyId();
698 
699             _defaultRolesMap.put(
700                 companyId,
701                 new Role[] {
702                         RoleLocalServiceUtil.getRole(
703                             companyId, RoleConstants.COMMUNITY_MEMBER),
704                         RoleLocalServiceUtil.getRole(
705                             companyId, RoleConstants.ORGANIZATION_MEMBER),
706                         RoleLocalServiceUtil.getRole(
707                             companyId, RoleConstants.POWER_USER),
708                     }
709                 );
710 
711             Role guestRole = RoleLocalServiceUtil.getRole(
712                 companyId, RoleConstants.GUEST);
713 
714             _guestRolesMap.put(companyId, guestRole);
715 
716             Role ownerRole = RoleLocalServiceUtil.getRole(
717                 companyId, RoleConstants.OWNER);
718 
719             _ownerRolesMap.put(companyId, ownerRole);
720 
721             long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
722                 companyId);
723 
724             _guestUsersSet.add(defaultUserId);
725         }
726 
727         // Roles_Permissions
728 
729         Connection con = null;
730         PreparedStatement ps = null;
731         ResultSet rs = null;
732 
733         try {
734             con = DataAccess.getConnection();
735 
736             ps = con.prepareStatement("SELECT * FROM Roles_Permissions");
737 
738             rs = ps.executeQuery();
739 
740             while (rs.next()) {
741                 long roleId = rs.getLong("roleId");
742                 long permissionId = rs.getLong("permissionId");
743 
744                 _rolesPermissions.add(roleId + "_" + permissionId);
745             }
746         }
747         finally {
748             DataAccess.cleanUp(con, ps, rs);
749         }
750 
751         // Groups
752 
753         List<Group> groups = GroupLocalServiceUtil.getGroups(
754             QueryUtil.ALL_POS, QueryUtil.ALL_POS);
755 
756         for (Group group : groups) {
757             _groupsMap.put(group.getGroupId(), group);
758         }
759     }
760 
761     protected boolean isGenerateCustomRoles() {
762         String[] parameterValues = getParameterValues();
763 
764         return GetterUtil.getBoolean(parameterValues[0]);
765     }
766 
767     private static final String _EXT_OTHER_ROLES = ".others_roles";
768 
769     private static final String _EXT_RESOURCE_PERMISSION =
770         ".resource_permission";
771 
772     private static final String _EXT_ROLE = ".role";
773 
774     private static final String _EXT_ROLES_PERMIMISSIONS = ".roles_permissions";
775 
776     private static final String _UPDATED = ".updated";
777 
778     private static Log _log = LogFactoryUtil.getLog(
779         ConvertPermissionAlgorithm.class);
780 
781     private Map<Long, Role[]> _defaultRolesMap = new HashMap<Long, Role[]>();
782     private Map<Long, Group> _groupsMap = new HashMap<Long, Group>();
783     private Map<Long, Role> _guestRolesMap = new HashMap<Long, Role>();
784     private Set<Long> _guestUsersSet = new HashSet<Long>();
785     private Map<Long, Role> _ownerRolesMap = new HashMap<Long, Role>();
786     private Set<String> _rolesPermissions = new HashSet<String>();
787 
788 }