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