001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsRoleEditList.java,v $
003: * Date : $Date: 2008-02-27 12:05:26 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.accounts;
033:
034: import org.opencms.file.CmsGroup;
035: import org.opencms.file.CmsUser;
036: import org.opencms.i18n.CmsMessageContainer;
037: import org.opencms.jsp.CmsJspActionElement;
038: import org.opencms.main.CmsException;
039: import org.opencms.main.CmsRuntimeException;
040: import org.opencms.main.OpenCms;
041: import org.opencms.security.CmsRole;
042: import org.opencms.util.CmsUUID;
043: import org.opencms.workplace.list.CmsListColumnAlignEnum;
044: import org.opencms.workplace.list.CmsListColumnDefinition;
045: import org.opencms.workplace.list.CmsListDirectAction;
046: import org.opencms.workplace.list.CmsListItem;
047: import org.opencms.workplace.list.CmsListItemActionIconComparator;
048: import org.opencms.workplace.list.CmsListItemDetails;
049: import org.opencms.workplace.list.CmsListMetadata;
050: import org.opencms.workplace.list.CmsListMultiAction;
051: import org.opencms.workplace.list.I_CmsListFormatter;
052:
053: import java.util.Iterator;
054: import java.util.List;
055: import java.util.Locale;
056:
057: import javax.servlet.http.HttpServletRequest;
058: import javax.servlet.http.HttpServletResponse;
059: import javax.servlet.jsp.PageContext;
060:
061: /**
062: * User roles overview view.<p>
063: *
064: * @author Raphael Schnuck
065: *
066: * @version $Revision: 1.5 $
067: *
068: * @since 6.5.6
069: */
070: public class CmsRoleEditList extends A_CmsRolesList {
071:
072: /** list action id constant. */
073: public static final String LIST_ACTION_ACTIVATE = "aa";
074:
075: /** list action id constant. */
076: public static final String LIST_ACTION_DEACTIVATE = "ac";
077:
078: /** list column id constant. */
079: public static final String LIST_COLUMN_ACTIVATE = "ca";
080:
081: /** list id constant. */
082: public static final String LIST_ID = "lsre";
083:
084: /** list action id constant. */
085: public static final String LIST_MACTION_ACTIVATE = "ma";
086:
087: /** list action id constant. */
088: public static final String LIST_MACTION_DEACTIVATE = "mc";
089:
090: /** Stores the value of the request parameter for the user id. */
091: private String m_paramUserid;
092:
093: /**
094: * Public constructor.<p>
095: *
096: * @param jsp an initialized JSP action element
097: */
098: public CmsRoleEditList(CmsJspActionElement jsp) {
099:
100: this (jsp, LIST_ID);
101: }
102:
103: /**
104: * Public constructor.<p>
105: *
106: * @param jsp an initialized JSP action element
107: * @param listId the id of the list
108: */
109: public CmsRoleEditList(CmsJspActionElement jsp, String listId) {
110:
111: this (jsp, listId, Messages.get().container(
112: Messages.GUI_ROLEEDIT_LIST_NAME_0));
113: }
114:
115: /**
116: * Public constructor.<p>
117: *
118: * @param jsp an initialized JSP action element
119: * @param listId the id of the list
120: * @param listName the name of the list
121: */
122: public CmsRoleEditList(CmsJspActionElement jsp, String listId,
123: CmsMessageContainer listName) {
124:
125: super (jsp, listId, listName);
126: }
127:
128: /**
129: * Public constructor with JSP variables.<p>
130: *
131: * @param context the JSP page context
132: * @param req the JSP request
133: * @param res the JSP response
134: */
135: public CmsRoleEditList(PageContext context, HttpServletRequest req,
136: HttpServletResponse res) {
137:
138: this (new CmsJspActionElement(context, req, res));
139: }
140:
141: /**
142: * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
143: */
144: public void executeListMultiActions() throws CmsRuntimeException {
145:
146: if (getParamListAction().equals(LIST_MACTION_ACTIVATE)) {
147: // execute the activate multiaction
148: try {
149: CmsUser user = getCms().readUser(
150: new CmsUUID(getParamUserid()));
151: Iterator itItems = getSelectedItems().iterator();
152: while (itItems.hasNext()) {
153: CmsListItem listItem = (CmsListItem) itItems.next();
154: CmsGroup group = getCms().readGroup(
155: (String) listItem
156: .get(LIST_COLUMN_GROUP_NAME));
157: CmsRole role = CmsRole.valueOf(group);
158: if (!OpenCms.getRoleManager().hasRole(getCms(),
159: user.getName(), role)) {
160: OpenCms.getRoleManager().addUserToRole(
161: getCms(), role, user.getName());
162: getCms().writeUser(user);
163: }
164: }
165: } catch (CmsException e) {
166: throw new CmsRuntimeException(Messages.get().container(
167: Messages.ERR_ACTIVATE_SELECTED_USERS_0), e);
168: }
169: // refreshing no needed becaus the activate action does not add/remove rows to the list
170: } else if (getParamListAction().equals(LIST_MACTION_DEACTIVATE)) {
171: // execute the activate multiaction
172: try {
173: CmsUser user = getCms().readUser(
174: new CmsUUID(getParamUserid()));
175: Iterator itItems = getSelectedItems().iterator();
176: while (itItems.hasNext()) {
177: CmsListItem listItem = (CmsListItem) itItems.next();
178: CmsGroup group = getCms().readGroup(
179: (String) listItem
180: .get(LIST_COLUMN_GROUP_NAME));
181: CmsRole role = CmsRole.valueOf(group);
182: if (OpenCms.getRoleManager().hasRole(getCms(),
183: user.getName(), role)) {
184: OpenCms.getRoleManager().removeUserFromRole(
185: getCms(), role, user.getName());
186: getCms().writeUser(user);
187: }
188: }
189: } catch (CmsException e) {
190: throw new CmsRuntimeException(Messages.get().container(
191: Messages.ERR_DEACTIVATE_SELECTED_USERS_0), e);
192: }
193: // refreshing no needed becaus the activate action does not add/remove rows to the list
194: } else {
195: throwListUnsupportedActionException();
196: }
197: listSave();
198: }
199:
200: /**
201: * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
202: */
203: public void executeListSingleActions() throws CmsRuntimeException {
204:
205: String roleName = getSelectedItem().getId();
206: try {
207: CmsRole role = CmsRole
208: .valueOf(getCms().readGroup(roleName));
209: CmsUser user = getCms().readUser(
210: new CmsUUID(getParamUserid()));
211:
212: if (getParamListAction().equals(LIST_ACTION_ACTIVATE)) {
213: // execute the activate action
214: try {
215: OpenCms.getRoleManager().addUserToRole(getCms(),
216: role, user.getName());
217: getCms().writeUser(user);
218: } catch (CmsException e) {
219: throw new CmsRuntimeException(Messages.get()
220: .container(Messages.ERR_ACTIVATE_ROLE_2,
221: roleName, user.getName()), e);
222: }
223: } else if (getParamListAction().equals(
224: LIST_ACTION_DEACTIVATE)) {
225: // execute the activate action
226: if (!OpenCms.getRoleManager().getRolesOfUser(getCms(),
227: user.getName(), "", true, true, true).contains(
228: role)) {
229: throw new CmsRuntimeException(
230: Messages
231: .get()
232: .container(
233: Messages.ERR_DEACTIVATE_INDIRECT_ROLE_2,
234: roleName, user.getName()));
235: }
236: try {
237: OpenCms.getRoleManager().removeUserFromRole(
238: getCms(), role, user.getName());
239: getCms().writeUser(user);
240: } catch (CmsException e) {
241: throw new CmsRuntimeException(Messages.get()
242: .container(Messages.ERR_DEACTIVATE_ROLE_2,
243: roleName, user.getName()), e);
244: }
245: } else {
246: throwListUnsupportedActionException();
247: }
248: listSave();
249: } catch (CmsException e) {
250: // should never happen
251: }
252: }
253:
254: /**
255: * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getIconPath(CmsListItem)
256: */
257: public String getIconPath(CmsListItem item) {
258:
259: try {
260: CmsRole role = CmsRole.valueOf(getCms().readGroup(
261: (String) item.get(LIST_COLUMN_GROUP_NAME)));
262: if (OpenCms.getRoleManager().hasRole(
263: getCms(),
264: getCms().readUser(new CmsUUID(getParamUserid()))
265: .getName(), role)) {
266: return PATH_BUTTONS + "role.png";
267: }
268: } catch (Exception e) {
269: // ignore
270: }
271: return PATH_BUTTONS + "role_inactive.png";
272: }
273:
274: /**
275: * Returns the User id parameter value.<p>
276: *
277: * @return the User id parameter value
278: */
279: public String getParamUserid() {
280:
281: return m_paramUserid;
282: }
283:
284: /**
285: * Sets the User id parameter value.<p>
286: *
287: * @param userid the userid to set
288: */
289: public void setParamUserid(String userid) {
290:
291: m_paramUserid = userid;
292: }
293:
294: /**
295: * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
296: */
297: protected String defaultActionHtmlStart() {
298:
299: return getList().listJs() + dialogContentStart(getParamTitle());
300: }
301:
302: /**
303: * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getRoles()
304: */
305: protected List getRoles() throws CmsException {
306:
307: return OpenCms.getRoleManager().getRolesOfUser(getCms(),
308: getCms().getRequestContext().currentUser().getName(),
309: getParamOufqn(), false, false, true);
310: }
311:
312: /**
313: * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
314: */
315: protected void setColumns(CmsListMetadata metadata) {
316:
317: super .setColumns(metadata);
318:
319: // create column for activation/deactivation
320: CmsListColumnDefinition actCol = new CmsListColumnDefinition(
321: LIST_COLUMN_ACTIVATE);
322: actCol.setName(Messages.get().container(
323: Messages.GUI_ROLEEDIT_LIST_COLS_ACTIVATE_0));
324: actCol.setHelpText(Messages.get().container(
325: Messages.GUI_ROLEEDIT_LIST_COLS_ACTIVATE_HELP_0));
326: actCol.setWidth("20");
327: actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
328: actCol
329: .setListItemComparator(new CmsListItemActionIconComparator());
330:
331: // activate action
332: CmsListDirectAction actAction = new CmsListDirectAction(
333: LIST_ACTION_ACTIVATE) {
334:
335: /**
336: * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
337: */
338: public boolean isVisible() {
339:
340: if (getItem() != null) {
341: String roleName = getItem().getId();
342: try {
343: CmsRole role = CmsRole.valueOf(getCms()
344: .readGroup(roleName));
345: if (OpenCms
346: .getRoleManager()
347: .hasRole(
348: getCms(),
349: getCms()
350: .readUser(
351: new CmsUUID(
352: ((CmsRoleEditList) getWp())
353: .getParamUserid()))
354: .getName(), role)) {
355: return false;
356: }
357: return true;
358: } catch (CmsException e) {
359: return false;
360: }
361: }
362: return super .isVisible();
363: }
364: };
365: actAction.setName(Messages.get().container(
366: Messages.GUI_ROLEEDIT_LIST_ACTION_ACTIVATE_NAME_0));
367: actAction.setHelpText(Messages.get().container(
368: Messages.GUI_ROLEEDIT_LIST_ACTION_ACTIVATE_HELP_0));
369: actAction.setIconPath(ICON_INACTIVE);
370: actCol.addDirectAction(actAction);
371:
372: // deactivate action
373: CmsListDirectAction deactAction = new CmsListDirectAction(
374: LIST_ACTION_DEACTIVATE) {
375:
376: /**
377: * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
378: */
379: public boolean isVisible() {
380:
381: if (getItem() != null) {
382: String roleName = getItem().getId();
383: try {
384: CmsRole role = CmsRole.valueOf(getCms()
385: .readGroup(roleName));
386: if (OpenCms
387: .getRoleManager()
388: .hasRole(
389: getCms(),
390: getCms()
391: .readUser(
392: new CmsUUID(
393: ((CmsRoleEditList) getWp())
394: .getParamUserid()))
395: .getName(), role)) {
396: return true;
397: }
398: return false;
399: } catch (CmsException e) {
400: return false;
401: }
402: }
403: return super .isVisible();
404: }
405: };
406: deactAction.setName(Messages.get().container(
407: Messages.GUI_ROLEEDIT_LIST_ACTION_DEACTIVATE_NAME_0));
408: deactAction.setHelpText(Messages.get().container(
409: Messages.GUI_ROLEEDIT_LIST_ACTION_DEACTIVATE_HELP_0));
410: deactAction.setIconPath(ICON_ACTIVE);
411: actCol.addDirectAction(deactAction);
412:
413: // add it to the list definition
414: metadata.addColumn(actCol, 1);
415: }
416:
417: /**
418: * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
419: */
420: protected void setIndependentActions(CmsListMetadata metadata) {
421:
422: // add description details
423: CmsListItemDetails descriptionDetails = new CmsListItemDetails(
424: LIST_DETAIL_DESCRIPTION);
425: descriptionDetails.setAtColumn(LIST_COLUMN_NAME);
426: descriptionDetails.setVisible(true);
427: descriptionDetails.setShowActionName(Messages.get().container(
428: Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_NAME_0));
429: descriptionDetails
430: .setShowActionHelpText(Messages
431: .get()
432: .container(
433: Messages.GUI_ROLEEDIT_DETAIL_SHOW_DESCRIPTION_HELP_0));
434: descriptionDetails.setHideActionName(Messages.get().container(
435: Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_NAME_0));
436: descriptionDetails
437: .setHideActionHelpText(Messages
438: .get()
439: .container(
440: Messages.GUI_ROLEEDIT_DETAIL_HIDE_DESCRIPTION_HELP_0));
441: descriptionDetails.setName(Messages.get().container(
442: Messages.GUI_ROLEEDIT_DETAIL_DESCRIPTION_NAME_0));
443: descriptionDetails.setFormatter(new I_CmsListFormatter() {
444:
445: /**
446: * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
447: */
448: public String format(Object data, Locale locale) {
449:
450: StringBuffer html = new StringBuffer(512);
451: html
452: .append("<table border='0' cellspacing='0' cellpadding='0'>\n");
453: html.append("\t<tr>\n");
454: html.append("\t\t<td style='white-space:normal;' >\n");
455: html.append("\t\t\t");
456: html.append(data == null ? "" : data);
457: html.append("\n");
458: html.append("\t\t</td>\n");
459: html.append("\t</tr>\n");
460: html.append("</table>\n");
461: return html.toString();
462: }
463: });
464: metadata.addItemDetails(descriptionDetails);
465: }
466:
467: /**
468: * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
469: */
470: protected void setMultiActions(CmsListMetadata metadata) {
471:
472: // add the activate role multi action
473: CmsListMultiAction activateRole = new CmsListMultiAction(
474: LIST_MACTION_ACTIVATE);
475: activateRole.setName(Messages.get().container(
476: Messages.GUI_ROLEEDIT_LIST_MACTION_ACTIVATE_NAME_0));
477: activateRole.setHelpText(Messages.get().container(
478: Messages.GUI_ROLEEDIT_LIST_MACTION_ACTIVATE_HELP_0));
479: activateRole.setIconPath(ICON_MULTI_ACTIVATE);
480: metadata.addMultiAction(activateRole);
481:
482: // add the deactivate role multi action
483: CmsListMultiAction deactivateRole = new CmsListMultiAction(
484: LIST_MACTION_DEACTIVATE);
485: deactivateRole.setName(Messages.get().container(
486: Messages.GUI_ROLEEDIT_LIST_MACTION_DEACTIVATE_NAME_0));
487: deactivateRole.setHelpText(Messages.get().container(
488: Messages.GUI_ROLEEDIT_LIST_MACTION_DEACTIVATE_HELP_0));
489: deactivateRole.setIconPath(ICON_MULTI_DEACTIVATE);
490: metadata.addMultiAction(deactivateRole);
491: }
492: }
|