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