BrightSide Workbench Full Report + Source Code
ContactAsUser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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 
19 package org.turro.contacts.util;
20 
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.amic.util.string.Strings;
26 import org.turro.action.Plugins;
27 import org.turro.contacts.Contact;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.security.IUser;
30 import org.turro.plugin.contacts.IContact;
31 
36 public class ContactAsUser extends DefaultContact implements IUser {
37 
39  protected boolean superUser = false;
40  //private UserProfileMap upm;
41 
42  @Override
43  public String getName() {
44  String name = super.getName();
45  return Strings.isBlank(name) ? "#" : name;
46  }
47 
48  @Override
49  public String getId() {
50  String id = super.getId();
51  return Strings.isBlank(id) ? "#" : id;
52  }
53 
54  @Override
55  public boolean validate(String login, String password) {
56  login = login.trim();
57  password = password.trim();
58  if(Strings.isBlank(login) || Strings.isBlank(password)) return false;
59  Map<String, Object> args = new HashMap<>();
60  args.put("login", login);
61  args.put("password", password);
62  args = Plugins.execute("SuperUser", args);
63  if(Boolean.TRUE.equals(args.get("su"))) {
64  superUser = true;
65  return true;
66  }
67  IContact ic;
68  if(login.contains("@")) {
69  ic = loadByEmail(login);
70  if(ic != null) {
71  contact = (Contact) ic.getContact();
72  }
73 // } else {
74 // loadByLogin(login);
75  }
76  if(isValid() && checkPassword(password)) {
77  //upm = getProfileMap();
79  return true;
80  } else {
81  contact = null;
82  }
83  return false;
84  }
85 
86  @Override
87  public boolean validate(String key) {
88  throw new UnsupportedOperationException("Temporal keys no longer supported.");
89  }
90 
91  @Override
92  public boolean validate2(String password2) {
93  return checkPassword(password2);
94  }
95 
96  @Override
97  public boolean impersonate(String login) {
98  IContact ic;
99  if(login.contains("@")) {
100  ic = loadByEmail(login);
101  if(ic != null) {
102  contact = (Contact) ic.getContact();
103  }
104  } else {
105  loadById(login);
106  }
107  if(isValid()) {
108  //upm = getProfileMap();
110  return true;
111  }
112  return false;
113  }
114 
115  @Override
116  public boolean impersonateByEmail(String email) {
117  return impersonate(email);
118  }
119 
120  @Override
121  public void reload() {
122  loadById(getId());
123  if(isValid()) {
124  //upm = getProfileMap();
126  }
127  }
128 
129  @Override
130  public boolean isThisUser(String id) {
131  return !Strings.isBlank(id) && (id.equals(contact.getId()) || id.equals(contact.getLogin()));
132  }
133 
134  @Override
135  public String getProperty(String key) {
136  return getConnector(key);
137  }
138 
139  @Override
140  public boolean isInRole(String role) {
141  if(superUser) return true;
142  //return upm.isInRole(role);
143  return getPermissions().isInRole(role);
144  }
145 
146  @Override
147  public boolean hasAnyRoleKey(String role) {
148  if(superUser) return true;
149  return getPermissions().anyKeyMath(Strings.csvToList(role));
150 // String[] value = role.split(" *, *");
151 // for(String s : value) {
152 // if(upm.containsKey(s)) {
153 // return true;
154 // }
155 // }
156 // return false;
157  }
158 
159  @Override
160  public Map getByDescriptors(String value) {
161  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
162  }
163 
164  @Override
165  public boolean hasRoleName(String role) {
166  if(superUser) return true;
167  return getRoles().contains(role);
168  }
169 
170  @Override
171  public List getUsers() {
172  throw new UnsupportedOperationException("Not supported yet.");
173  }
174 
175  @Override
176  public String createTmpKey(Date valid) {
177  throw new UnsupportedOperationException("Temporal keys no longer supported.");
178  }
179 
180  @Override
182  this.constructor = constructor;
183  }
184 
185 }
static Map< String, Object > execute(String name, Map params)
Definition: Plugins.java:45
void setConstructor(IConstructor constructor)
boolean validate(String login, String password)
boolean impersonateByEmail(String email)
boolean validate2(String password2)
boolean checkPassword(String password)
boolean anyKeyMath(List< String > roles)