/**
* 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.password.policies.admin.web.internal.portlet;
import com.liferay.password.policies.admin.constants.PasswordPoliciesAdminPortletKeys;
import com.liferay.portal.kernel.exception.DuplicatePasswordPolicyException;
import com.liferay.portal.kernel.exception.NoSuchPasswordPolicyException;
import com.liferay.portal.kernel.exception.PasswordPolicyNameException;
import com.liferay.portal.kernel.exception.RequiredPasswordPolicyException;
import com.liferay.portal.kernel.model.PasswordPolicy;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.service.OrganizationService;
import com.liferay.portal.kernel.service.PasswordPolicyService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextFactory;
import com.liferay.portal.kernel.service.UserService;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.Http;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Brian Wing Shun Chan
* @author Scott Lee
* @author Drew Brokke
*/
@Component(
immediate = true,
property = {
"com.liferay.portlet.css-class-wrapper=portlet-users-admin",
"com.liferay.portlet.display-category=category.hidden",
"com.liferay.portlet.icon=/icons/password_policies_admin.png",
"com.liferay.portlet.preferences-owned-by-group=true",
"com.liferay.portlet.private-request-attributes=false",
"com.liferay.portlet.private-session-attributes=false",
"com.liferay.portlet.render-weight=50",
"com.liferay.portlet.use-default-template=true",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + PasswordPoliciesAdminPortletKeys.PASSWORD_POLICIES_ADMIN,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=administrator",
"javax.portlet.supports.mime-type=text/html"
},
service = Portlet.class
)
public class PasswordPoliciesAdminPortlet extends MVCPortlet {
public void deletePasswordPolicies(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long[] passwordPolicyIds = StringUtil.split(
ParamUtil.getString(actionRequest, "passwordPolicyIds"), 0L);
for (long passwordPolicyId : passwordPolicyIds) {
_passwordPolicyService.deletePasswordPolicy(passwordPolicyId);
}
}
public void deletePasswordPolicy(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long passwordPolicyId = ParamUtil.getLong(
actionRequest, "passwordPolicyId");
_passwordPolicyService.deletePasswordPolicy(passwordPolicyId);
}
public void editPasswordPolicy(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long passwordPolicyId = ParamUtil.getLong(
actionRequest, "passwordPolicyId");
String name = ParamUtil.getString(actionRequest, "name");
String description = ParamUtil.getString(actionRequest, "description");
boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
boolean changeRequired = false;
long minAge = 0;
if (changeable) {
changeRequired = ParamUtil.getBoolean(
actionRequest, "changeRequired");
minAge = ParamUtil.getLong(actionRequest, "minAge");
}
boolean checkSyntax = ParamUtil.getBoolean(
actionRequest, "checkSyntax");
boolean allowDictionaryWords = ParamUtil.getBoolean(
actionRequest, "allowDictionaryWords");
int minAlphanumeric = ParamUtil.getInteger(
actionRequest, "minAlphanumeric");
int minLength = ParamUtil.getInteger(actionRequest, "minLength");
int minLowerCase = ParamUtil.getInteger(actionRequest, "minLowerCase");
int minNumbers = ParamUtil.getInteger(actionRequest, "minNumbers");
int minSymbols = ParamUtil.getInteger(actionRequest, "minSymbols");
int minUpperCase = ParamUtil.getInteger(actionRequest, "minUpperCase");
String regex = ParamUtil.getString(actionRequest, "regex");
boolean history = ParamUtil.getBoolean(actionRequest, "history");
int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
long lockoutDuration = ParamUtil.getLong(
actionRequest, "lockoutDuration");
long resetFailureCount = ParamUtil.getLong(
actionRequest, "resetFailureCount");
long resetTicketMaxAge = ParamUtil.getLong(
actionRequest, "resetTicketMaxAge");
ServiceContext serviceContext = ServiceContextFactory.getInstance(
PasswordPolicy.class.getName(), actionRequest);
if (passwordPolicyId <= 0) {
// Add password policy
PasswordPolicy passwordPolicy =
_passwordPolicyService.addPasswordPolicy(
name, description, changeable, changeRequired, minAge,
checkSyntax, allowDictionaryWords, minAlphanumeric,
minLength, minLowerCase, minNumbers, minSymbols,
minUpperCase, regex, history, historyCount, expireable,
maxAge, warningTime, graceLimit, lockout, maxFailure,
lockoutDuration, resetFailureCount, resetTicketMaxAge,
serviceContext);
passwordPolicyId = passwordPolicy.getPasswordPolicyId();
}
else {
// Update password policy
_passwordPolicyService.updatePasswordPolicy(
passwordPolicyId, name, description, changeable, changeRequired,
minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
regex, history, historyCount, expireable, maxAge, warningTime,
graceLimit, lockout, maxFailure, lockoutDuration,
resetFailureCount, resetTicketMaxAge, serviceContext);
}
String redirect = ParamUtil.getString(actionRequest, "redirect");
if (Validator.isNotNull(redirect)) {
redirect = _http.setParameter(
redirect, actionResponse.getNamespace() + "passwordPolicyId",
passwordPolicyId);
actionRequest.setAttribute(WebKeys.REDIRECT, redirect);
}
}
public void editPasswordPolicyAssignments(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long passwordPolicyId = ParamUtil.getLong(
actionRequest, "passwordPolicyId");
long[] addUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addUserIds"), 0L);
long[] removeUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
long[] addOrganizationIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
long[] removeOrganizationIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
if (ArrayUtil.isNotEmpty(addUserIds)) {
_userService.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
}
if (ArrayUtil.isNotEmpty(removeUserIds)) {
_userService.unsetPasswordPolicyUsers(
passwordPolicyId, removeUserIds);
}
if (ArrayUtil.isNotEmpty(addOrganizationIds)) {
_organizationService.addPasswordPolicyOrganizations(
passwordPolicyId, addOrganizationIds);
}
if (ArrayUtil.isNotEmpty(removeOrganizationIds)) {
_organizationService.unsetPasswordPolicyOrganizations(
passwordPolicyId, removeOrganizationIds);
}
}
@Override
protected void doDispatch(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
if (SessionErrors.contains(
renderRequest, NoSuchPasswordPolicyException.class.getName()) ||
SessionErrors.contains(
renderRequest, PrincipalException.getNestedClasses())) {
include("/error.jsp", renderRequest, renderResponse);
}
else if (SessionErrors.contains(
renderRequest,
DuplicatePasswordPolicyException.class.getName()) ||
SessionErrors.contains(
renderRequest,
PasswordPolicyNameException.class.getName())) {
include("/edit_password_policy.jsp", renderRequest, renderResponse);
}
else {
super.doDispatch(renderRequest, renderResponse);
}
}
@Override
protected boolean isSessionErrorException(Throwable cause) {
if (cause instanceof DuplicatePasswordPolicyException ||
cause instanceof NoSuchPasswordPolicyException ||
cause instanceof PasswordPolicyNameException ||
cause instanceof PrincipalException ||
cause instanceof RequiredPasswordPolicyException) {
return true;
}
return false;
}
@Reference(unbind = "-")
protected void setOrganizationService(
OrganizationService organizationService) {
_organizationService = organizationService;
}
@Reference(unbind = "-")
protected void setPasswordPolicyService(
PasswordPolicyService passwordPolicyService) {
_passwordPolicyService = passwordPolicyService;
}
@Reference(unbind = "-")
protected void setUserService(UserService userService) {
_userService = userService;
}
@Reference
private Http _http;
private OrganizationService _organizationService;
private PasswordPolicyService _passwordPolicyService;
private UserService _userService;
}