Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.service.impl;
import com.liferay.exportimport.kernel.lar.ExportImportThreadLocal;
import com.liferay.exportimport.kernel.staging.MergeLayoutPrototypesThreadLocal;
import com.liferay.petra.sql.dsl.DSLQueryFactoryUtil;
import com.liferay.petra.sql.dsl.query.DSLQuery;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.kernel.exception.NoSuchResourceActionException;
import com.liferay.portal.kernel.exception.NoSuchResourcePermissionException;
import com.liferay.portal.kernel.exception.NoSuchRoleException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.internal.service.permission.ModelPermissionsImpl;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.AuditedModel;
import com.liferay.portal.kernel.model.GroupedModel;
import com.liferay.portal.kernel.model.Portlet;
import com.liferay.portal.kernel.model.Resource;
import com.liferay.portal.kernel.model.ResourceAction;
import com.liferay.portal.kernel.model.ResourceConstants;
import com.liferay.portal.kernel.model.ResourcePermission;
import com.liferay.portal.kernel.model.ResourcePermissionConstants;
import com.liferay.portal.kernel.model.ResourcePermissionTable;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.role.RoleConstants;
import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.security.permission.PermissionUpdateHandler;
import com.liferay.portal.kernel.security.permission.PermissionUpdateHandlerRegistryUtil;
import com.liferay.portal.kernel.security.permission.ResourceActionsUtil;
import com.liferay.portal.kernel.service.ResourceActionLocalService;
import com.liferay.portal.kernel.service.RoleLocalService;
import com.liferay.portal.kernel.service.SQLStateAcceptor;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.permission.ModelPermissions;
import com.liferay.portal.kernel.service.permission.ModelPermissionsFactory;
import com.liferay.portal.kernel.service.persistence.ResourceActionPersistence;
import com.liferay.portal.kernel.service.persistence.RolePersistence;
import com.liferay.portal.kernel.spring.aop.Property;
import com.liferay.portal.kernel.spring.aop.Retry;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.ServiceProxyFactory;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.impl.ResourceImpl;
import com.liferay.portal.security.permission.PermissionCacheUtil;
import com.liferay.portal.service.base.ResourcePermissionLocalServiceBaseImpl;
import com.liferay.portal.util.PropsValues;
import com.liferay.util.dao.orm.CustomSQLUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Provides the local service for accessing, adding, checking, deleting,
* granting, and revoking resource permissions.
*
*
* Before attempting to read any of the documentation for this class, first read
* {@link com.liferay.portal.model.impl.ResourcePermissionImpl} for an
* explanation of scoping.
*
*
* @author Brian Wing Shun Chan
* @author Raymond Augé
* @author Connor McKay
*/
public class ResourcePermissionLocalServiceImpl
extends ResourcePermissionLocalServiceBaseImpl {
@Override
public void addModelResourcePermissions(
AuditedModel auditedModel, ServiceContext serviceContext)
throws PortalException {
ModelPermissions modelPermissions =
serviceContext.getModelPermissions();
if (_matches(modelPermissions, auditedModel.getModelClassName())) {
ModelPermissionsImpl.setUsed(modelPermissions);
addModelResourcePermissions(
auditedModel.getCompanyId(), getGroupId(auditedModel),
auditedModel.getUserId(), auditedModel.getModelClassName(),
String.valueOf(auditedModel.getPrimaryKeyObj()),
modelPermissions);
}
else if (serviceContext.isAddGroupPermissions() ||
serviceContext.isAddGuestPermissions()) {
long groupId = 0;
if (serviceContext.isAddGroupPermissions()) {
groupId = getGroupId(auditedModel);
}
addResourcePermissions(
auditedModel.getCompanyId(), groupId, auditedModel.getUserId(),
auditedModel.getModelClassName(),
String.valueOf(auditedModel.getPrimaryKeyObj()), false,
serviceContext);
}
else {
if (serviceContext.isDeriveDefaultPermissions()) {
serviceContext.deriveDefaultPermissions(
getGroupId(auditedModel), auditedModel.getModelClassName());
}
addModelResourcePermissions(
auditedModel.getCompanyId(), getGroupId(auditedModel),
auditedModel.getUserId(), auditedModel.getModelClassName(),
String.valueOf(auditedModel.getPrimaryKeyObj()),
serviceContext.getModelPermissions());
}
}
@Override
public void addModelResourcePermissions(
long companyId, long groupId, long userId, String name,
String primKey, ModelPermissions modelPermissions)
throws PortalException {
if (!PermissionThreadLocal.isAddResource()) {
return;
}
validate(name, false);
if (primKey == null) {
return;
}
if (!_matches(modelPermissions, name)) {
modelPermissions = ModelPermissionsFactory.create(name);
modelPermissions.addRolePermissions(
RoleConstants.OWNER, new String[0]);
}
ModelPermissionsImpl.setUsed(modelPermissions);
// Individual Permissions
boolean flushResourcePermissionEnabled =
PermissionThreadLocal.isFlushResourcePermissionEnabled(
name, primKey);
PermissionThreadLocal.setFlushResourcePermissionEnabled(
name, primKey, false);
try {
// Owner permissions
Role ownerRole = _roleLocalService.getRole(
companyId, RoleConstants.OWNER);
List ownerActionIds =
ResourceActionsUtil.getModelResourceActions(name);
filterOwnerActions(name, ownerActionIds);
String[] ownerPermissions = ownerActionIds.toArray(new String[0]);
setOwnerResourcePermissions(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey,
ownerRole.getRoleId(), userId, ownerPermissions);
for (String roleName : modelPermissions.getRoleNames()) {
Role role = getRole(companyId, groupId, roleName);
setResourcePermissions(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
primKey, role.getRoleId(),
modelPermissions.getActionIds(roleName));
}
}
finally {
PermissionThreadLocal.setFlushResourcePermissionEnabled(
name, primKey, flushResourcePermissionEnabled);
PermissionCacheUtil.clearResourcePermissionCache(
ResourceConstants.SCOPE_INDIVIDUAL, name, primKey);
IndexWriterHelperUtil.updatePermissionFields(name, primKey);
}
}
/**
* Adds resources for the model with the name and primary key string, always
* creating a resource at the individual scope and only creating resources
* at the group, group template, and company scope if such resources don't
* already exist.
*
* @param companyId the primary key of the portal instance
* @param groupId the primary key of the group
* @param userId the primary key of the user adding the resources
* @param name a name for the resource, typically the model's class name
* @param primKey the primary key string of the model instance, optionally
* an empty string if no instance exists
* @param groupPermissions the group permissions to be applied
* @param guestPermissions the guest permissions to be applied
*/
@Override
public void addModelResourcePermissions(
long companyId, long groupId, long userId, String name,
String primKey, String[] groupPermissions,
String[] guestPermissions)
throws PortalException {
ModelPermissions modelPermissions = ModelPermissionsFactory.create(
groupPermissions, guestPermissions, name);
addModelResourcePermissions(
companyId, groupId, userId, name, primKey, modelPermissions);
}
/**
* Grants the role permission at the scope to perform the action on
* resources of the type. Existing actions are retained.
*
*
* This method cannot be used to grant individual scope permissions, but is
* only intended for adding permissions at the company, group, and
* group-template scopes. For example, this method could be used to grant a
* company scope permission to edit message board posts.
*
*
*
* If a company scope permission is granted to resources that the role
* already had group scope permissions to, the group scope permissions are
* deleted. Likewise, if a group scope permission is granted to resources
* that the role already had company scope permissions to, the company scope
* permissions are deleted. Be aware that this latter behavior can result in
* an overall reduction in permissions for the role.
*
*
*
* Depending on the scope, the value of primKey will have
* different meanings. For more information, see {@link
* com.liferay.portal.model.impl.ResourcePermissionImpl}.
*
*
* @param companyId the primary key of the company
* @param name the resource's name, which can be either a class name or a
* portlet ID
* @param scope the scope. This method only supports company, group, and
* group-template scope.
* @param primKey the primary key
* @param roleId the primary key of the role
* @param actionId the action ID
*/
@Override
@Retry(
acceptor = SQLStateAcceptor.class,
properties = {
@Property(
name = SQLStateAcceptor.SQLSTATE,
value = SQLStateAcceptor.SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION
)
}
)
public void addResourcePermission(
long companyId, String name, int scope, String primKey, long roleId,
String actionId)
throws PortalException {
if (scope == ResourceConstants.SCOPE_COMPANY) {
// Remove group permission
removeResourcePermissions(
companyId, name, ResourceConstants.SCOPE_GROUP, roleId,
actionId);
}
else if (scope == ResourceConstants.SCOPE_GROUP) {
// Remove company permission
removeResourcePermissions(
companyId, name, ResourceConstants.SCOPE_COMPANY, roleId,
actionId);
}
else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
throw new NoSuchResourcePermissionException();
}
updateResourcePermission(
companyId, name, scope, primKey, roleId, 0, new String[] {actionId},
ResourcePermissionConstants.OPERATOR_ADD);
}
/**
* Adds resources for the entity with the name and primary key string,
* always creating a resource at the individual scope and only creating
* resources at the group, group template, and company scope if such
* resources don't already exist.
*
* @param companyId the primary key of the portal instance
* @param groupId the primary key of the group
* @param userId the primary key of the user adding the resources
* @param name a name for the resource, which should be a portlet ID if the
* resource is a portlet or the resource's class name otherwise
* @param primKey the primary key string of the resource instance,
* optionally an empty string if no instance exists
* @param portletActions whether to associate portlet actions with the
* resource
*/
@Override
public void addResourcePermissions(
long companyId, long groupId, long userId, String name,
String primKey, boolean portletActions,
ServiceContext serviceContext)
throws PortalException {
if (!PermissionThreadLocal.isAddResource()) {
return;
}
validate(name, portletActions);
if (primKey == null) {
return;
}
// Permissions
boolean flushResourcePermissionEnabled =
PermissionThreadLocal.isFlushResourcePermissionEnabled(
name, primKey);
PermissionThreadLocal.setFlushResourcePermissionEnabled(
name, primKey, false);
List resourcePermissions =
resourcePermissionPersistence.findByC_N_S_P(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
Map resourcePermissionsMap =
_getResourcePermissionsMap(resourcePermissions);
boolean modified = false;
try {
List actionIds = null;
if (portletActions) {
actionIds = ResourceActionsUtil.getPortletResourceActions(name);
}
else {
actionIds = ResourceActionsUtil.getModelResourceActions(name);
filterOwnerActions(name, actionIds);
}
Role role = _roleLocalService.getRole(
companyId, RoleConstants.OWNER);
if (_updateResourcePermission(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
primKey, userId, role.getRoleId(), Boolean.FALSE,
actionIds.toArray(new String[0]),
ResourcePermissionConstants.OPERATOR_SET, true,
resourcePermissionsMap)) {
modified = true;
}
// Group permissions
if ((groupId > 0) && (serviceContext != null) &&
serviceContext.isAddGroupPermissions()) {
List actions = null;
if (portletActions) {
actions =
ResourceActionsUtil.
getPortletResourceGroupDefaultActions(name);
}
else {
actions =
ResourceActionsUtil.getModelResourceGroupDefaultActions(
name);
}
Role groupRole = _roleLocalService.getDefaultGroupRole(groupId);
if (_updateResourcePermission(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
primKey, 0, groupRole.getRoleId(), Boolean.FALSE,
actions.toArray(new String[0]),
ResourcePermissionConstants.OPERATOR_SET, true,
resourcePermissionsMap)) {
modified = true;
}
}
// Guest permissions
if ((serviceContext != null) &&
serviceContext.isAddGuestPermissions()) {
// Don't add guest permissions when you've already added group
// permissions and the given group is the guest group.
List actions = null;
if (portletActions) {
actions =
ResourceActionsUtil.
getPortletResourceGuestDefaultActions(name);
}
else {
actions =
ResourceActionsUtil.getModelResourceGuestDefaultActions(
name);
}
Role guestRole = _roleLocalService.getRole(
companyId, RoleConstants.GUEST);
if (_updateResourcePermission(
companyId, name, ResourceConstants.SCOPE_INDIVIDUAL,
primKey, 0, guestRole.getRoleId(), Boolean.TRUE,
actions.toArray(new String[0]),
ResourcePermissionConstants.OPERATOR_SET, true,
resourcePermissionsMap)) {
modified = true;
}
}
}
finally {
PermissionThreadLocal.setFlushResourcePermissionEnabled(
name, primKey, flushResourcePermissionEnabled);
if (modified) {
PermissionCacheUtil.clearResourcePermissionCache(
ResourceConstants.SCOPE_INDIVIDUAL, name, primKey);
if ((serviceContext == null) ||
serviceContext.isIndexingEnabled()) {
IndexWriterHelperUtil.updatePermissionFields(name, primKey);
}
}
}
}
/**
* Grants the role permissions at the scope to perform the actions on all
* resources of the type. Existing actions are retained.
*
*
* This method should only be used to add default permissions to existing
* resources en masse during upgrades or while verifying permissions. For
* example, this method could be used to grant site members individual scope
* permissions to view all blog posts.
*
*
* @param resourceName the resource's name, which can be either a class name
* or a portlet ID
* @param roleName the role's name
* @param scope the scope
* @param resourceActionBitwiseValue the bitwise IDs of the actions
*/
@Override
public void addResourcePermissions(
String resourceName, String roleName, int scope,
long resourceActionBitwiseValue) {
List roles = _rolePersistence.findByName(roleName);
if (roles.isEmpty()) {
return;
}
Session session = resourcePermissionPersistence.openSession();
try {
// Update existing resource permissions
String sql = CustomSQLUtil.get(_UPDATE_ACTION_IDS);
sql = StringUtil.replace(
sql, "[$ROLE_ID$]",
ListUtil.toString(roles, Role.ROLE_ID_ACCESSOR));
SQLQuery sqlQuery = session.createSynchronizedSQLQuery(sql);
QueryPos queryPos = QueryPos.getInstance(sqlQuery);
queryPos.add(resourceActionBitwiseValue);
queryPos.add(resourceActionBitwiseValue);
queryPos.add(resourceName);
queryPos.add(scope);
sqlQuery.executeUpdate();
// Add missing resource permissions
sql = CustomSQLUtil.get(_FIND_MISSING_RESOURCE_PERMISSIONS);
sqlQuery = session.createSynchronizedSQLQuery(sql);
sqlQuery.addScalar("companyId", Type.LONG);
sqlQuery.addScalar("name", Type.STRING);
sqlQuery.addScalar("scope", Type.INTEGER);
sqlQuery.addScalar("primKey", Type.STRING);
sqlQuery.addScalar("roleId", Type.LONG);
queryPos = QueryPos.getInstance(sqlQuery);
queryPos.add(resourceName);
queryPos.add(scope);
queryPos.add(roleName);
List