BrightSide Workbench Full Report + Source Code
UserAsContact.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.plugin.contacts;
19 
20 import java.io.IOException;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.annotation.ElephantContact;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.impl.context.ContextFactory;
29 import org.turro.elephant.security.IUser;
30 import org.turro.elephant.security.IUserAdmin;
31 
36 @ElephantContact(priority=2)
37 public class UserAsContact implements IContact {
38 
39  private IUser user;
40  private Application app;
41 
42  @Override
43  public Object loadById(String id) {
44  user = ContextFactory.getUser(null);
45  user.impersonate(id);
46  return user;
47  }
48 
49  @Override
50  public Object loadByLogin(String login) {
51  user = ContextFactory.getUser(null);
52  user.impersonate(login);
53  return user;
54  }
55 
56  @Override
57  public Object loadLogged(Application app) {
58  user = app.getConstructor().getUser();
59  return user;
60  }
61 
62  @Override
63  public void setContact(Object contact) {
64  if(contact instanceof IUser) {
65  user = (IUser) contact;
66  }
67  }
68 
69  @Override
70  public Object getContact() {
71  return user;
72  }
73 
74  @Override
75  public String getId() {
76  return user != null ? user.getId() : null;
77  }
78 
79  @Override
80  public String getGlobalId() {
81  return null;
82  }
83 
84  @Override
85  public String getLogin() {
86  return user != null ? user.getId() : null;
87  }
88 
89  @Override
90  public String getName() {
91  return user != null ? user.getName() : null;
92  }
93 
94  @Override
95  public String getConnector(String id) {
96  return user.getProperty(id);
97  }
98 
99  @Override
100  public List<IContact> getRelations(String id) {
101  return new ArrayList<IContact>();
102  }
103 
104  @Override
105  public void setConnector(String id, String value) {
106  IUserAdmin uAdmin = ContextFactory.getUserAdmin(app.getConstructor());
107  if(id != null && value != null) {
108  try {
109  uAdmin.setProperty(user.getId(), id, value);
110  } catch (IOException ex) {
111  Logger.getLogger(UserAsContact.class.getName()).log(Level.SEVERE, null, ex);
112  }
113  }
114  }
115 
116  @Override
117  public void setApplication(Application app) {
118  this.app = app;
119  }
120 
121  @Override
122  public void setName(String value) {
123  IUserAdmin uAdmin = ContextFactory.getUserAdmin(app.getConstructor());
124  if (value != null) {
125  try {
126  uAdmin.setName(user.getId(), value);
127  } catch (IOException ex) {
128  Logger.getLogger(UserAsContact.class.getName()).log(Level.SEVERE, null, ex);
129  }
130  }
131  }
132 
133  @Override
134  public void setPassword(String newValue, String repeatValue) {
135  IUserAdmin uAdmin = ContextFactory.getUserAdmin(app.getConstructor());
136  if(newValue != null && newValue.trim().length() > 0 && newValue.equals(repeatValue)) {
137  try {
138  uAdmin.setPasswords(user.getId(), newValue, null);
139  } catch (IOException ex) {
140  Logger.getLogger(UserAsContact.class.getName()).log(Level.SEVERE, null, ex);
141  }
142  }
143  }
144 
145  @Override
146  public void applyChanges() {
147 
148  }
149 
150  @Override
151  public boolean isValid() {
152  return user != null && user.isValid();
153  }
154 
155  @Override
156  public boolean checkPassword(String password) {
157  return user.validate(user.getId(), password);
158  }
159 
160  @Override
161  public String getCommentValue(String key) {
162  return null;
163  }
164 
165  @Override
166  public Object getObject(String key) {
167  return null;
168  }
169 
170  @Override
171  public void setObject(String key, Serializable value) {
172  }
173 
174  @Override
175  public boolean isWebUser() {
176  return true;
177  }
178 
179  @Override
180  public List<IContact> loadByPartialName(String name) {
181  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
182  }
183 
184  @Override
185  public IContact loadByName(String name) {
186  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
187  }
188 
189  @Override
190  public void setAttribute(String key, Object value) {
191  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
192  }
193 
194  @Override
195  public Object getAttribute(String key) {
196  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
197  }
198 
199 }
boolean checkPassword(String password)
void setAttribute(String key, Object value)
void setObject(String key, Serializable value)
void setConnector(String id, String value)
List< IContact > getRelations(String id)
void setPassword(String newValue, String repeatValue)
List< IContact > loadByPartialName(String name)
void setPasswords(String id, String pass1, String pass2)
void setProperty(String id, String name, String value)
void setName(String id, String name)
boolean impersonate(String login)
String getProperty(String key)
boolean validate(String login, String password)