BrightSide Workbench Full Report + Source Code
SocialGroups.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.security;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import org.turro.auth.Authentication;
28 import org.turro.contacts.Contact;
29 import org.turro.contacts.ContactType;
30 import org.turro.contacts.Syndication;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.sql.SqlClause;
34 import org.turro.tags.Tags;
35 
40 public class SocialGroups extends SecurityGroups {
41 
42  public static Collection<SecurityGroup> allowedSocialGroups() {
44  }
45 
46  public static Collection<SecurityGroup> allowedSocialGroups(IContact contact) {
48  if(contact.isAdmin()) {
49  return instance.socialGroups.values();
50  } else {
51  Set<SecurityGroup> socials = new HashSet<>();
52  contact.getSyndications().forEach(syndication -> {
53  socials.addAll(instance.socialGroups.get(syndication).getAllowed().stream()
54  .map(asg -> instance.socialGroups.get(asg.getId())).collect(Collectors.toList()));
55  });
56  return socials;
57  }
58  }
59 
60  public static void syndicate(List<Contact> contacts, Set<String> socialGroupIds, Dao dao) {
61  SocialGroups socials = instance();
62  contacts.forEach(contact -> {
63  socials.synchronizeSocials(contact, socials.flatSocialGroups(contact, socialGroupIds));
64  if(dao != null) {
65  dao.saveObject(contact);
66  }
67  });
68  }
69 
70  public static void unsyndicate(List<Contact> contacts, Set<String> socialGroupIds, Dao dao) {
71  SocialGroups socials = instance();
72  contacts.forEach(contact -> {
73  socials.removeSocials(contact, socials.flatSocialGroups(null, socialGroupIds));
74  if(dao != null) {
75  dao.saveObject(contact);
76  }
77  });
78  }
79 
80  public static void absoluteSyndicate(List<Contact> contacts, Set<String> socialGroupIds, Dao dao) {
81  SocialGroups socials = instance();
82  contacts.forEach(contact -> {
83  socials.synchronizeSocials(contact, socials.flatSocialGroups(null, socialGroupIds));
84  if(dao != null) {
85  dao.saveObject(contact);
86  }
87  });
88  }
89 
90  public static void resyndicate(List<Contact> contacts, Dao dao) {
91  SocialGroups socials = instance();
92  Set<String> socialGroupIds = new HashSet<>();
93  contacts.forEach(contact -> {
94  socials.synchronizeSocials(contact, socials.flatSocialGroups(contact, socialGroupIds));
95  if(dao != null) {
96  dao.saveObject(contact);
97  }
98  });
99  }
100 
101  /* Inheritance */
102 
103  public static void checkInheritance(List<Contact> contacts, Dao dao) {
104  SocialGroups socials = instance();
105  contacts.forEach(contact -> {
106  socials.checkInheritance(contact, dao);
107  });
108  }
109 
110  /* Utils */
111 
112  public void removeSocials(Contact contact, Set<String> socialGroupIds) {
113  contact.getSyndications().removeIf(syndication -> socialGroupIds.contains(syndication.getName()));
114  }
115 
116  public void synchronizeSocials(Contact contact, Set<String> socialGroupIds) {
117  contact.getSyndications().removeIf(syndication -> !socialGroupIds.contains(syndication.getName()));
118  Set<String> syndicationNames = contact.getSyndications().stream()
119  .map(syndication -> syndication.getName()).collect(Collectors.toSet());
120  socialGroupIds.stream().filter(social -> !syndicationNames.contains(social)).forEach(social -> {
121  Syndication syndication = new Syndication();
122  syndication.setContact(contact);
123  syndication.setName(social);
124  contact.getSyndications().add(syndication);
125  });
126  }
127 
128  @Deprecated
129  public void synchronizeTags(Contact contact, Set<String> tags) {
130  Set<String> currentTags = Tags.getTags(contact).stream()
131  .map(tag -> tag.getTagName()).collect(Collectors.toSet());
132  currentTags.stream().filter(tag -> !tags.contains(tag) && possibleTag(tag)).forEach(tag -> {
133  Tags.removeTag(contact, tag);
134  });
135  tags.stream().filter(tag -> !currentTags.contains(tag)).forEach(tag -> {
136  Tags.addTag(contact, tag);
137  });
138  }
139 
140  public Set<String> flatSocialGroups(Contact contact, Set<String> socials) {
141  Set<String> flatSocials = new HashSet<>();
142  if(contact != null) {
143  flatSocials.addAll(contact.getSyndications().stream()
144  .map(syndication -> syndication.getName()).collect(Collectors.toSet()));
145  }
146  socials.forEach(social -> {
147  recurseSocialGroups(flatSocials, socialGroups.get(social));
148  });
149  return flatSocials;
150  }
151 
152  public Set<String> flatRoles(Set<Syndication> syndications) {
153  Set<String> flatRoles = new HashSet<>();
154  syndications.forEach(syndication -> {
155  SecurityGroup sg = socialGroups.get(syndication.getName());
156  if(sg != null) {
157  flatRoles.addAll(sg.getRoles());
158  }
159  });
160  return flatRoles;
161  }
162 
163  @Deprecated
164  public Set<String> flatTags(Set<Syndication> syndications) {
165  Set<String> flatTags = new HashSet<>();
166  syndications.forEach(syndication -> {
167  SecurityGroup sg = socialGroups.get(syndication.getName());
168  if(sg != null) {
169  flatTags.addAll(sg.getTags());
170  }
171  });
172  return flatTags;
173  }
174 
175  public void checkInheritance(Contact contact, Dao dao) {
176  if(ContactType.juridicals().contains(contact.getType())) {
177  contact.getWorkerSet().getWorkers().forEach(staff -> {
178  checkCompanyInheritance(staff, dao);
179  });
180  } else {
181  checkCompanyInheritance(contact, dao);
182  }
183  }
184 
186  final Contact current = dao.find(Contact.class, contact.getId());
187  if(current != null) {
188  List<String> inheritance = getInheritedFrom(current);
189  // Delete dynamically assigned
190  SqlClause.delete("Syndication")
191  .where().notIn("name", inheritance)
192  .and().equal("dynamic", true)
193  .and().equal("contact", current)
194  .dao(dao)
195  .execute();
196  // Dynamize existing
197  SqlClause.update("Syndication")
198  .set("dynamic", true)
199  .where().in("name", inheritance)
200  .and().equal("contact", current)
201  .dao(dao)
202  .execute();
203  // Add missing
204  List<String> currentNames = SqlClause.select("s.name").from("Syndication s")
205  .where().equal("contact", current)
206  .dao(dao)
207  .resultList(String.class);
208  inheritance.stream().filter(name -> !currentNames.contains(name))
209  .forEach(name -> {
210  Syndication syndication = new Syndication();
211  syndication.setContact(current);
212  syndication.setName(name);
213  syndication.setDynamic(true);
214  dao.saveObject(syndication);
215  });
216  return dao.find(Contact.class, current.getId());
217  } else {
218  return contact;
219  }
220 
221  }
222 
223  public List<String> getInheritedFrom(Contact contact) {
224  List<String> inheritance = new ArrayList<>();
225  if(contact != null) {
226  contact.getBusinessSet().getBusinessList().forEach(business -> {
227  inheritance.addAll(business.getSyndications().stream()
228  .filter(syndication -> syndication.getSocialGroup().isInherits())
229  .map(syndication -> syndication.getName())
230  .toList());
231  });
232  }
233  return inheritance;
234  }
235  /* Factory */
236 
237  public static SocialGroups instance() {
238  return new SocialGroups();
239  }
240 
241 }
Set< Syndication > getSyndications()
Definition: Contact.java:403
void setDynamic(boolean dynamic)
void setContact(org.turro.contacts.Contact contact)
final Map< String, SecurityGroup > socialGroups
void recurseSocialGroups(Set< String > flatSocials, SecurityGroup sg)
static Collection< SecurityGroup > allowedSocialGroups()
static Collection< SecurityGroup > allowedSocialGroups(IContact contact)
List< String > getInheritedFrom(Contact contact)
void synchronizeSocials(Contact contact, Set< String > socialGroupIds)
static void unsyndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
Set< String > flatSocialGroups(Contact contact, Set< String > socials)
static void absoluteSyndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
void synchronizeTags(Contact contact, Set< String > tags)
static void resyndicate(List< Contact > contacts, Dao dao)
Set< String > flatTags(Set< Syndication > syndications)
static void checkInheritance(List< Contact > contacts, Dao dao)
Set< String > flatRoles(Set< Syndication > syndications)
Contact checkCompanyInheritance(Contact contact, Dao dao)
static void syndicate(List< Contact > contacts, Set< String > socialGroupIds, Dao dao)
static SocialGroups instance()
void removeSocials(Contact contact, Set< String > socialGroupIds)
void checkInheritance(Contact contact, Dao dao)
static void addTag(Object entity, String tagName)
Definition: Tags.java:188
static void removeTag(Object entity, String tagName)
Definition: Tags.java:215
static TagSet getTags(Object entity)
Definition: Tags.java:351
static EnumSet< ContactType > juridicals()