001: package org.contineo.web.admin;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005:
006: import org.contineo.core.FileBean;
007: import org.contineo.core.communication.EMail;
008: import org.contineo.core.communication.dao.EMailDAO;
009: import org.contineo.core.document.History;
010: import org.contineo.core.document.dao.HistoryDAO;
011: import org.contineo.core.security.SecurityManager;
012: import org.contineo.core.security.User;
013: import org.contineo.core.security.UserDoc;
014: import org.contineo.core.security.dao.MenuDAO;
015: import org.contineo.core.security.dao.UserDAO;
016: import org.contineo.core.security.dao.UserDocDAO;
017:
018: import org.contineo.util.Context;
019: import org.contineo.util.config.SettingsConfig;
020:
021: import org.contineo.web.SessionManagement;
022: import org.contineo.web.i18n.Messages;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import javax.faces.context.FacesContext;
029:
030: /**
031: * <p>
032: * The <code>UsersRecordsManager</code> class is responsible for constructing
033: * the list of <code>User</code> beans which will be bound to a ice:dataTable
034: * JSF component. <p/>
035: * <p>
036: * Large data sets could be handle by adding a ice:dataPaginator. Alternatively
037: * the dataTable could also be hidden and the dataTable could be added to
038: * scrollable ice:panelGroup.
039: * </p>
040: *
041: * @author Marco Meschieri
042: * @version $Id: DocumentsRecordsManager.java,v 1.1 2007/06/29 06:28:29 marco
043: * Exp $
044: * @since 3.0
045: */
046: public class UsersRecordsManager {
047: protected static Log log = LogFactory
048: .getLog(UsersRecordsManager.class);
049: private Collection<User> users = new ArrayList<User>();
050: private String selectedPanel = "list";
051:
052: public UsersRecordsManager() {
053: }
054:
055: public void reload() {
056: users.clear();
057:
058: try {
059: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
060: MenuDAO.class);
061: String uname = SessionManagement.getUsername();
062:
063: if (mdao.isReadEnable(6, uname)) {
064: UserDAO dao = (UserDAO) Context.getInstance().getBean(
065: UserDAO.class);
066: users = dao.findAll();
067: } else {
068: Messages.addLocalizedError("errors.noaccess");
069: }
070: } catch (Exception e) {
071: log.error(e.getMessage(), e);
072: Messages.addLocalizedError("errors.error");
073: }
074: }
075:
076: public String getSelectedPanel() {
077: return selectedPanel;
078: }
079:
080: public void setSelectedPanel(String panel) {
081: this .selectedPanel = panel;
082: }
083:
084: public String addUser() {
085: selectedPanel = "add";
086:
087: UserForm userForm = ((UserForm) FacesContext
088: .getCurrentInstance().getApplication()
089: .createValueBinding("#{userForm}").getValue(
090: FacesContext.getCurrentInstance()));
091: userForm.setUser(new User());
092:
093: return null;
094: }
095:
096: public String edit() {
097: selectedPanel = "edit";
098:
099: UserForm userForm = ((UserForm) FacesContext
100: .getCurrentInstance().getApplication()
101: .createValueBinding("#{userForm}").getValue(
102: FacesContext.getCurrentInstance()));
103: User user = (User) FacesContext.getCurrentInstance()
104: .getExternalContext().getRequestMap().get("item");
105: userForm.setUser(user);
106:
107: return null;
108: }
109:
110: public String password() {
111: selectedPanel = "passwd";
112:
113: UserForm userForm = ((UserForm) FacesContext
114: .getCurrentInstance().getApplication()
115: .createValueBinding("#{userForm}").getValue(
116: FacesContext.getCurrentInstance()));
117: User user = (User) FacesContext.getCurrentInstance()
118: .getExternalContext().getRequestMap().get("item");
119: userForm.setUser(user);
120:
121: return null;
122: }
123:
124: public String list() {
125: selectedPanel = "list";
126: reload();
127:
128: return null;
129: }
130:
131: /**
132: * Gets the list of UserRecord which will be used by the ice:dataTable
133: * component.
134: */
135: public Collection<User> getUsers() {
136: if (users.size() == 0) {
137: reload();
138: }
139:
140: return users;
141: }
142:
143: public int getCount() {
144: return getUsers().size();
145: }
146:
147: public String delete() {
148: User user = (User) FacesContext.getCurrentInstance()
149: .getExternalContext().getRequestMap().get("item");
150:
151: if (SessionManagement.isValid()) {
152: try {
153: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
154: MenuDAO.class);
155: String uname = SessionManagement.getUsername();
156: UserDAO dao = (UserDAO) Context.getInstance().getBean(
157: UserDAO.class);
158: SecurityManager manager = (SecurityManager) Context
159: .getInstance().getBean(SecurityManager.class);
160:
161: // if we do not have write permissions, we do not have to do any
162: // further steps
163: if (mdao.isReadEnable(10, uname)) {
164: // get the user's groups and check if he is member of
165: // "admin" group
166: User toBeDeletedUser = dao.findByPrimaryKey(user
167: .getUserName());
168: boolean isAdmin = false;
169:
170: if (toBeDeletedUser != null) {
171: toBeDeletedUser.initGroupNames();
172:
173: String[] userGroups = toBeDeletedUser
174: .getGroupNames();
175:
176: if (userGroups != null) {
177: for (int i = 0; i < userGroups.length; i++) {
178: if (userGroups[i].equals("admin")) {
179: isAdmin = true;
180:
181: break;
182: }
183: }
184: }
185: }
186:
187: // if the user is member of "admin", we have to check that
188: // he is not the last user in that group;
189: // here we count how many users still belong to group admin
190: int adminsFound = 0;
191:
192: if (isAdmin) {
193: Collection allUsers = dao.findAll();
194: Iterator userIter = allUsers.iterator(); // get all
195: // users
196:
197: while (userIter.hasNext()) {
198: User currUser = (User) userIter.next();
199: currUser.initGroupNames(); // we always to call
200: // this before accessing
201: // the groups
202:
203: String[] groups = currUser.getGroupNames();
204:
205: if (groups != null) {
206: for (int i = 0; i < groups.length; i++) {
207: if (groups[i].equals("admin")) {
208: adminsFound++;
209:
210: break; // for performance reasons we
211: // break if we found enough
212: // users
213: }
214: }
215: }
216:
217: // basically we are just interested that there are
218: // at least 2 users,
219: // so we can safely delete one
220: if (adminsFound > 2) {
221: break;
222: }
223: }
224: }
225:
226: // now we can try to delete the user
227: if (!isAdmin || (isAdmin && (adminsFound > 1))) {
228: // delete emails and email accounts
229: EMailDAO emailDao = (EMailDAO) Context
230: .getInstance().getBean(EMailDAO.class);
231: Collection coll = emailDao.findByUserName(user
232: .getUserName());
233: Iterator iter = coll.iterator();
234:
235: while (iter.hasNext()) {
236: EMail email = (EMail) iter.next();
237: emailDao.delete(email.getMessageId());
238: }
239:
240: // delete user doc entries (recently accessed files)
241: UserDocDAO userDocDao = (UserDocDAO) Context
242: .getInstance()
243: .getBean(UserDocDAO.class);
244: Collection userDocColl = userDocDao
245: .findByUserName(user.getUserName());
246: Iterator userDocIter = userDocColl.iterator();
247:
248: while (userDocIter.hasNext()) {
249: UserDoc userDoc = (UserDoc) userDocIter
250: .next();
251: userDocDao.delete(user.getUserName(),
252: userDoc.getMenuId());
253: }
254:
255: // delete all history entries connected to this user
256: HistoryDAO historyDAO = (HistoryDAO) Context
257: .getInstance()
258: .getBean(HistoryDAO.class);
259: Collection historyColl = historyDAO
260: .findByUsername(user.getUserName());
261: Iterator historyIter = historyColl.iterator();
262:
263: while (historyIter.hasNext()) {
264: History history = (History) historyIter
265: .next();
266: historyDAO.delete(history.getHistoryId());
267: }
268:
269: manager
270: .removeUserFromAllGroups(toBeDeletedUser);
271:
272: boolean deleted = dao
273: .delete(user.getUserName());
274:
275: if (!deleted) {
276: Messages
277: .addLocalizedError("errors.action.deleteuser");
278: } else {
279: Messages
280: .addLocalizedInfo("msg.action.deleteuser");
281:
282: SettingsConfig conf = (SettingsConfig) Context
283: .getInstance().getBean(
284: SettingsConfig.class);
285: String userdir = conf.getValue("userdir")
286: + "/" + user.getUserName();
287: FileBean.deleteDir(userdir);
288: }
289: } else if (isAdmin && (adminsFound < 2)) {
290: Messages
291: .addLocalizedInfo("msg.action.deleteuser.admingroup");
292: }
293: } else {
294: return "login";
295: }
296: } catch (Exception e) {
297: log.error(e.getMessage(), e);
298: Messages.addLocalizedError("errors.action.deleteuser");
299: }
300: } else {
301: return "login";
302: }
303:
304: setSelectedPanel("list");
305: reload();
306:
307: return null;
308: }
309: }
|