BrightSide Workbench Full Report + Source Code
ContactToUser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.contacts.util;
19 
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.contacts.Connector;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.Role;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.elephant.impl.context.ContextFactory;
29 import org.turro.elephant.security.IUser;
30 import org.turro.elephant.security.IUserAdmin;
31 
36 @Deprecated
37 public class ContactToUser {
38 
39  private Contact contact;
40  private IConstructor constructor;
41 
42  public ContactToUser() {
43  }
44 
45  public ContactToUser(IConstructor constructor, Contact contact) {
46  setConstructor(constructor);
47  setContact(contact);
48  }
49 
50  public void setConstructor(IConstructor constructor) {
51  this.constructor = constructor;
52  }
53 
54  public void setContact(Contact contact) {
55  this.contact = contact;
56  }
57 
58  public boolean isLoginValid(String value) {
59  String login = contact.getLogin();
60  if (login == null || login.length() == 0) {
61  login = (String) value;
62  if (login.length() > 0) {
63  IUserAdmin uAdmin = ContextFactory.getUserAdmin(constructor);
64  if (uAdmin.checkId(login)) {
65  return false;
66  }
67  }
68  }
69  return true;
70  }
71 
72  public void doUserChanges() throws Exception {
73  String login = contact.getLogin();
74  if (login != null && login.length() > 0) {
75  IUserAdmin uAdmin = ContextFactory.getUserAdmin(constructor);
76  try {
77  if (uAdmin.checkId(login)) {
78  uAdmin.setName(login, contact.getName());
79  uAdmin.setPasswords(login, ElephantContext.decrypt(contact.getPass1()),
80  ElephantContext.decrypt(contact.getPass2()));
81  } else {
82  uAdmin.addUser(login, contact.getName(), ElephantContext.decrypt(contact.getPass1()),
83  ElephantContext.decrypt(contact.getPass2()));
84  }
85  uAdmin.removeProperty(login, IUser.CONNECTOR_EMAIL);
86  for (Connector c : contact.getConnectors()) {
87  if (c.getDescription().toLowerCase().equals(IUser.CONNECTOR_EMAIL.toLowerCase())) {
88  uAdmin.setProperty(login, IUser.CONNECTOR_EMAIL, c.getValue());
89  }
90  }
91  for (Connector c : contact.getConnectors()) {
92  if (c.getDescription().toLowerCase().equals(IUser.CONNECTOR_EMAIL_LOGIN.toLowerCase())) {
93  uAdmin.setProperty(login, IUser.CONNECTOR_EMAIL_LOGIN, c.getValue());
94  }
95  }
96  uAdmin.cleanRoles(login);
97  for (Role r : contact.getRoles()) {
98  uAdmin.setRole(login, r.getName());
99  }
100  } catch (IOException ex) {
101  Logger.getLogger(ContactToUser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  }
104  }
105 
106  public void doUserDeletions() {
107  String login = contact.getLogin();
108  if (login != null && login.length() > 0) {
109  IUserAdmin uAdmin = ContextFactory.getUserAdmin(constructor);
110  if (uAdmin.checkId(login)) {
111  try {
112  uAdmin.removeUser(login);
113  } catch (IOException ex) {
114  Logger.getLogger(ContactToUser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
115  }
116  }
117  }
118  }
119 }
Set< Role > getRoles()
Definition: Contact.java:406
Set< Connector > getConnectors()
Definition: Contact.java:358
void setConstructor(IConstructor constructor)
ContactToUser(IConstructor constructor, Contact contact)
static IUserAdmin getUserAdmin(IConstructor constructor)
void setPasswords(String id, String pass1, String pass2)
void removeProperty(String id, String name)
void addUser(String id, String name, String pass1, String pass2)
void setRole(String id, String name)
void setProperty(String id, String name, String value)
void setName(String id, String name)
static final String CONNECTOR_EMAIL
Definition: IUser.java:32
static final String CONNECTOR_EMAIL_LOGIN
Definition: IUser.java:35