1
22
23 package com.liferay.portlet.enterpriseadmin.action;
24
25 import com.liferay.portal.DuplicateRoleException;
26 import com.liferay.portal.NoSuchRoleException;
27 import com.liferay.portal.RequiredRoleException;
28 import com.liferay.portal.RoleNameException;
29 import com.liferay.portal.kernel.util.Constants;
30 import com.liferay.portal.kernel.util.ParamUtil;
31 import com.liferay.portal.model.impl.RoleImpl;
32 import com.liferay.portal.security.auth.PrincipalException;
33 import com.liferay.portal.service.RoleServiceUtil;
34 import com.liferay.portal.struts.PortletAction;
35 import com.liferay.util.servlet.SessionErrors;
36
37 import javax.portlet.ActionRequest;
38 import javax.portlet.ActionResponse;
39 import javax.portlet.PortletConfig;
40 import javax.portlet.RenderRequest;
41 import javax.portlet.RenderResponse;
42
43 import org.apache.struts.action.ActionForm;
44 import org.apache.struts.action.ActionForward;
45 import org.apache.struts.action.ActionMapping;
46
47
53 public class EditRoleAction extends PortletAction {
54
55 public void processAction(
56 ActionMapping mapping, ActionForm form, PortletConfig config,
57 ActionRequest req, ActionResponse res)
58 throws Exception {
59
60 String cmd = ParamUtil.getString(req, Constants.CMD);
61
62 try {
63 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
64 updateRole(req);
65 }
66 else if (cmd.equals(Constants.DELETE)) {
67 deleteRole(req);
68 }
69
70 sendRedirect(req, res);
71 }
72 catch (Exception e) {
73 if (e instanceof PrincipalException) {
74 SessionErrors.add(req, e.getClass().getName());
75
76 setForward(req, "portlet.enterprise_admin.error");
77 }
78 else if (e instanceof DuplicateRoleException ||
79 e instanceof NoSuchRoleException ||
80 e instanceof RequiredRoleException ||
81 e instanceof RoleNameException) {
82
83 SessionErrors.add(req, e.getClass().getName());
84
85 if (cmd.equals(Constants.DELETE)) {
86 res.sendRedirect(ParamUtil.getString(req, "redirect"));
87 }
88 }
89 else {
90 throw e;
91 }
92 }
93 }
94
95 public ActionForward render(
96 ActionMapping mapping, ActionForm form, PortletConfig config,
97 RenderRequest req, RenderResponse res)
98 throws Exception {
99
100 try {
101 ActionUtil.getRole(req);
102 }
103 catch (Exception e) {
104 if (e instanceof NoSuchRoleException ||
105 e instanceof PrincipalException) {
106
107 SessionErrors.add(req, e.getClass().getName());
108
109 return mapping.findForward("portlet.enterprise_admin.error");
110 }
111 else {
112 throw e;
113 }
114 }
115
116 return mapping.findForward(
117 getForward(req, "portlet.enterprise_admin.edit_role"));
118 }
119
120 protected void deleteRole(ActionRequest req) throws Exception {
121 long roleId = ParamUtil.getLong(req, "roleId");
122
123 RoleServiceUtil.deleteRole(roleId);
124 }
125
126 protected void updateRole(ActionRequest req) throws Exception {
127 long roleId = ParamUtil.getLong(req, "roleId");
128
129 String name = ParamUtil.getString(req, "name");
130 String description = ParamUtil.getString(req, "description");
131 int type = ParamUtil.getInteger(req, "type", RoleImpl.TYPE_REGULAR);
132
133 if (roleId <= 0) {
134
135
137 RoleServiceUtil.addRole(name, description, type);
138 }
139 else {
140
141
143 RoleServiceUtil.updateRole(roleId, name, description);
144 }
145 }
146
147 }