BrightSide Workbench Full Report + Source Code
ContactIdentity.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.marker;
20 
21 import java.io.File;
22 import org.turro.elephant.context.Application;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.plugin.contacts.IContact;
25 import org.turro.plugin.contacts.IProfile;
26 import org.turro.security.FlatPermissions;
27 import org.turro.security.PermissionMap;
28 
33 public class ContactIdentity {
34 
35  public boolean isAppAdministrator() {
36  return isContactAdministrator();
37  }
38 
39  public boolean isContactAdministrator() {
40  return Application.getApplication().isInRole("contact:all");
41  }
42 
43  public boolean isDossierAdministrator() {
44  return Application.getApplication().isInRole("dossier:all");
45  }
46 
47  public boolean isWebapp() {
48  IContact logged = (IContact) Application.getUser();
49  return logged != null && logged.isValid() && logged.isWebapp();
50  }
51 
52  public boolean isUserMenu() {
53  IContact logged = (IContact) Application.getUser();
54  return logged != null && logged.isValid() && logged.isUserMenu();
55  }
56 
57  public boolean isMember(String identity) {
58  return Application.getApplication().isInRole(identity + ":member");
59  }
60 
61  public boolean isContactLogged() {
62  return Application.getApplication().isInRole("user:in");
63  }
64 
65  public boolean isInRole(String role) {
66  return Application.getApplication().isInRole(role);
67  }
68 
69  public boolean isMe(IContact contact) {
70  IContact logged = (IContact) Application.getUser();
71  if(logged != null && logged.isValid() && contact != null && contact.isValid()) {
72  return logged.getId().equals(contact.getId());
73  }
74  return false;
75  }
76 
77  public String getAvatar() {
78  IContact logged = (IContact) Application.getUser();
79  if(logged != null && logged.isValid()) {
80  File favatar = new File(ElephantContext.getRealPath("/_internal/files/contact/" + logged.getId() + "/profile/face.png"));
81  if(favatar.exists()) {
82  return ElephantContext.getRootWebPath() + "/_internal/files/contact/" + logged.getId() + "/profile/face.png";
83  }
84  }
85  return null;
86  }
87 
88  public IProfile getProfile() {
89  IContact logged = (IContact) Application.getUser();
90  return logged != null && logged.isValid() ? logged.getProfile() : null;
91  }
92 
93  public String getName() {
94  IContact logged = (IContact) Application.getUser();
95  return logged != null && logged.isValid() ? logged.getName() : null;
96  }
97 
98  public String getEmail() {
99  IContact logged = (IContact) Application.getUser();
100  return logged != null && logged.isValid() ? logged.getEmail() : null;
101  }
102 
103  public String getPhone() {
104  IContact logged = (IContact) Application.getUser();
105  return logged != null && logged.isValid() ? logged.getProfile().getPhone() : null;
106  }
107 
108  public IContact getContact() {
109  IContact logged = (IContact) Application.getUser();
110  return logged != null && logged.isValid() ? logged : null;
111  }
112 
113  private static PermissionMap EMPTY_PERMISSIONS = new PermissionMap(new FlatPermissions());
114 
116  IContact contact = getContact();
117  return contact != null ? contact.getPermissions().asMap() : EMPTY_PERMISSIONS;
118  }
119 
120 }
boolean isMember(String identity)
boolean isMe(IContact contact)