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