UserRolesController methods up
[portal.git] / portal-BE / src / main / java / org / onap / portal / domain / db / fn / FnUser.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.domain.db.fn;
42
43 import java.io.Serializable;
44 import java.time.LocalDateTime;
45 import java.util.Collection;
46 import java.util.HashSet;
47 import java.util.Iterator;
48 import java.util.Set;
49 import java.util.SortedSet;
50 import java.util.TreeSet;
51 import java.util.stream.Collectors;
52 import javax.persistence.CascadeType;
53 import javax.persistence.Column;
54 import javax.persistence.Entity;
55 import javax.persistence.FetchType;
56 import javax.persistence.GeneratedValue;
57 import javax.persistence.GenerationType;
58 import javax.persistence.Id;
59 import javax.persistence.Index;
60 import javax.persistence.JoinColumn;
61 import javax.persistence.ManyToMany;
62 import javax.persistence.ManyToOne;
63 import javax.persistence.NamedQueries;
64 import javax.persistence.NamedQuery;
65 import javax.persistence.OneToMany;
66 import javax.persistence.Table;
67 import javax.persistence.UniqueConstraint;
68 import javax.validation.constraints.Digits;
69 import javax.validation.constraints.Email;
70 import javax.validation.constraints.NotNull;
71 import javax.validation.constraints.PastOrPresent;
72 import javax.validation.constraints.Size;
73 import lombok.AllArgsConstructor;
74 import lombok.Builder;
75 import lombok.Getter;
76 import lombok.NoArgsConstructor;
77 import lombok.Setter;
78 import org.hibernate.annotations.DynamicUpdate;
79 import org.hibernate.validator.constraints.SafeHtml;
80 import org.onap.portal.domain.db.cr.CrReportFileHistory;
81 import org.onap.portal.domain.db.ep.EpPersUserWidgetPlacement;
82 import org.onap.portal.domain.db.ep.EpPersUserWidgetSel;
83 import org.onap.portal.domain.db.ep.EpUserNotification;
84 import org.onap.portal.domain.db.ep.EpUserRolesRequest;
85 import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter;
86 import org.onap.portal.domain.dto.DomainVo;
87 import org.onap.portalsdk.core.domain.App;
88 import org.onap.portalsdk.core.domain.Role;
89 import org.onap.portalsdk.core.domain.UserApp;
90 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
91 import org.springframework.security.core.GrantedAuthority;
92 import org.springframework.security.core.authority.SimpleGrantedAuthority;
93 import org.springframework.security.core.userdetails.UserDetails;
94
95 /*
96 CREATE TABLE `fn_user` (
97         `user_id` int(11) NOT NULL AUTO_INCREMENT,
98         `org_id` int(11) DEFAULT NULL,
99         `manager_id` int(11) DEFAULT NULL,
100         `first_name` varchar(50) DEFAULT NULL,
101         `middle_name` varchar(50) DEFAULT NULL,
102         `last_name` varchar(50) DEFAULT NULL,
103         `phone` varchar(25) DEFAULT NULL,
104         `fax` varchar(25) DEFAULT NULL,
105         `cellular` varchar(25) DEFAULT NULL,
106         `email` varchar(50) DEFAULT NULL,
107         `address_id` decimal(11,0) DEFAULT NULL,
108         `alert_method_cd` varchar(10) DEFAULT NULL,
109         `hrid` varchar(20) DEFAULT NULL,
110         `org_user_id` varchar(20) DEFAULT NULL,
111         `org_code` varchar(30) DEFAULT NULL,
112         `login_id` varchar(25) DEFAULT NULL,
113         `login_pwd` varchar(100) DEFAULT NULL,
114         `last_login_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
115         `active_yn` varchar(1) NOT NULL DEFAULT 'y',
116         `created_id` int(11) DEFAULT NULL,
117         `created_date` timestamp NOT NULL DEFAULT current_timestamp(),
118         `modified_id` int(11) DEFAULT NULL,
119         `modified_date` timestamp NOT NULL DEFAULT current_timestamp(),
120         `is_internal_yn` char(1) NOT NULL DEFAULT 'n',
121         `address_line_1` varchar(100) DEFAULT NULL,
122         `address_line_2` varchar(100) DEFAULT NULL,
123         `city` varchar(50) DEFAULT NULL,
124         `state_cd` varchar(3) DEFAULT NULL,
125         `zip_code` varchar(11) DEFAULT NULL,
126         `country_cd` varchar(3) DEFAULT NULL,
127         `location_clli` varchar(8) DEFAULT NULL,
128         `org_manager_userid` varchar(20) DEFAULT NULL,
129         `company` varchar(100) DEFAULT NULL,
130         `department_name` varchar(100) DEFAULT NULL,
131         `job_title` varchar(100) DEFAULT NULL,
132         `timezone` int(11) DEFAULT NULL,
133         `department` varchar(25) DEFAULT NULL,
134         `business_unit` varchar(25) DEFAULT NULL,
135         `business_unit_name` varchar(100) DEFAULT NULL,
136         `cost_center` varchar(25) DEFAULT NULL,
137         `fin_loc_code` varchar(10) DEFAULT NULL,
138         `silo_status` varchar(10) DEFAULT NULL,
139         `language_id` int(2) NOT NULL DEFAULT 1,
140         PRIMARY KEY (`user_id`),
141         UNIQUE KEY `fn_user_hrid` (`hrid`) USING BTREE,
142         UNIQUE KEY `fn_user_login_id` (`login_id`) USING BTREE,
143         KEY `fn_user_address_id` (`address_id`) USING BTREE,
144         KEY `fn_user_alert_method_cd` (`alert_method_cd`) USING BTREE,
145         KEY `fn_user_org_id` (`org_id`) USING BTREE,
146         KEY `fk_fn_user_ref_197_fn_user` (`manager_id`),
147         KEY `fk_fn_user_ref_198_fn_user` (`created_id`),
148         KEY `fk_fn_user_ref_199_fn_user` (`modified_id`),
149         KEY `fk_timezone` (`timezone`),
150         CONSTRAINT `fk_fn_user_ref_110_fn_org` FOREIGN KEY (`org_id`) REFERENCES `fn_org` (`org_id`),
151         CONSTRAINT `fk_fn_user_ref_123_fn_lu_al` FOREIGN KEY (`alert_method_cd`) REFERENCES `fn_lu_alert_method` (`alert_method_cd`),
152         CONSTRAINT `fk_fn_user_ref_197_fn_user` FOREIGN KEY (`manager_id`) REFERENCES `fn_user` (`user_id`),
153         CONSTRAINT `fk_fn_user_ref_198_fn_user` FOREIGN KEY (`created_id`) REFERENCES `fn_user` (`user_id`),
154         CONSTRAINT `fk_fn_user_ref_199_fn_user` FOREIGN KEY (`modified_id`) REFERENCES `fn_user` (`user_id`),
155         CONSTRAINT `fk_timezone` FOREIGN KEY (`timezone`) REFERENCES `fn_lu_timezone` (`timezone_id`)
156         )
157 */
158
159 @NamedQueries({
160     @NamedQuery(
161         name = "FnUser.getUserWithOrgUserId",
162         query = "FROM FnUser WHERE orgUserId = :orgId"),
163     @NamedQuery(
164         name = "FnUser.getActiveUsers",
165         query = "FROM FnUser WHERE activeYn = 'Y'"),
166     @NamedQuery(
167         name = "FnUser.getUsersByOrgIds",
168         query = "FROM FnUser WHERE orgUserId IN :orgIds"
169     )
170 })
171 @Table(name = "fn_user", indexes = {
172     @Index(name = "fn_user_address_id", columnList = "address_id"),
173     @Index(name = "fn_user_alert_method_cd", columnList = "alert_method_cd"),
174     @Index(name = "fn_user_org_id", columnList = "org_id"),
175     @Index(name = "fk_fn_user_ref_197_fn_user", columnList = "manager_id"),
176     @Index(name = "fk_fn_user_ref_198_fn_user", columnList = "created_id"),
177     @Index(name = "fk_fn_user_ref_199_fn_user", columnList = "modified_id"),
178     @Index(name = "fk_timezone", columnList = "timezone")
179 },
180     uniqueConstraints = {
181         @UniqueConstraint(name = "fn_user_hrid", columnNames = "hrid"),
182         @UniqueConstraint(name = "fn_user_login_id", columnNames = "login_id")
183
184     })
185
186 @Getter
187 @Setter
188 @Entity
189 @Builder
190 @NoArgsConstructor
191 @AllArgsConstructor
192 @DynamicUpdate
193 public class FnUser extends DomainVo implements UserDetails, Serializable {
194
195   private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnUser.class);
196
197   @Id
198   @GeneratedValue(strategy = GenerationType.IDENTITY)
199   @Column(name = "user_id", nullable = false)
200   private Long userId;
201   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
202   @JoinColumn(name = "org_id")
203   private FnOrg orgId;
204   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
205   @JoinColumn(name = "manager_id")
206   private FnUser managerId;
207   @Column(name = "first_name", length = 50)
208   @Size(max = 50)
209   @SafeHtml
210   private String firstName;
211   @Column(name = "middle_name", length = 50)
212   @Size(max = 50)
213   @SafeHtml
214   private String middleName;
215   @Column(name = "last_name", length = 50)
216   @Size(max = 50)
217   @SafeHtml
218   private String lastName;
219   @Column(name = "phone", length = 25)
220   @Size(max = 25)
221   @SafeHtml
222   private String phone;
223   @Column(name = "fax", length = 25)
224   @Size(max = 25)
225   @SafeHtml
226   private String fax;
227   @Column(name = "cellular", length = 25)
228   @Size(max = 25)
229   @SafeHtml
230   private String cellular;
231   @Column(name = "email", length = 50)
232   @Size(max = 50)
233   @Email
234   @SafeHtml
235   private String email;
236   @Column(name = "address_id")
237   @Digits(integer = 11, fraction = 0)
238   private Long addressId;
239   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
240   @JoinColumn(name = "alert_method_cd")
241   private FnLuAlertMethod alertMethodCd;
242   @Column(name = "hrid", length = 20)
243   @Size(max = 20)
244   @SafeHtml
245   private String hrid;
246   @Column(name = "org_user_id", length = 20)
247   @Size(max = 20)
248   @SafeHtml
249   private String orgUserId;
250   @Column(name = "org_code", length = 30)
251   @Size(max = 30)
252   @SafeHtml
253   private String org_code;
254   @Column(name = "login_id", length = 25)
255   @Size(max = 25)
256   @SafeHtml
257   private String loginId;
258   @Column(name = "login_pwd", length = 100)
259   @Size(max = 100)
260   @SafeHtml
261   private String loginPwd;
262   @Column(name = "last_login_date", nullable = false, columnDefinition = "datetime DEFAULT current_timestamp() ON UPDATE current_timestamp()")
263   @PastOrPresent
264   protected LocalDateTime lastLoginDate;
265   @Column(name = "active_yn", nullable = false)
266   private Boolean activeYn;
267   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
268   @JoinColumn(name = "created_id")
269   private FnUser createdId;
270   @Column(name = "created_date", columnDefinition = "datetime DEFAULT current_timestamp()", nullable = false)
271   @PastOrPresent
272   protected LocalDateTime createdDate;
273   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
274   @JoinColumn(name = "modified_id")
275   private FnUser modifiedId;
276   @Column(name = "modified_date", nullable = false, columnDefinition = "datetime default now()")
277   @PastOrPresent
278   protected LocalDateTime modifiedDate;
279   @Column(name = "is_internal_yn", nullable = false, columnDefinition = "bit DEFAULT 0")
280   private Boolean isInternalYn;
281   @Column(name = "is_system_user", nullable = false, columnDefinition = "bit DEFAULT 0")
282   private Boolean isSystemUser;
283   @Column(name = "address_line_1", length = 100)
284   @Size(max = 100)
285   @SafeHtml
286   private String addressLine1;
287   @Column(name = "address_line_2", length = 100)
288   @Size(max = 100)
289   @SafeHtml
290   private String addressLine2;
291   @Column(name = "city", length = 50)
292   @Size(max = 50)
293   @SafeHtml
294   private String city;
295   @Column(name = "state_cd", length = 3)
296   @Size(max = 3)
297   @SafeHtml
298   private String stateCd;
299   @Column(name = "zip_code", length = 11)
300   @Size(max = 11)
301   @SafeHtml
302   private String zipCode;
303   @Column(name = "country_cd", length = 3)
304   @Size(max = 3)
305   @SafeHtml
306   private String countryCd;
307   @Column(name = "location_clli", length = 8)
308   @Size(max = 8)
309   @SafeHtml
310   private String locationClli;
311   @Column(name = "org_manager_userid", length = 20)
312   @Size(max = 20)
313   @SafeHtml
314   private String orgManagerUserId;
315   @Column(name = "company", length = 100)
316   @Size(max = 100)
317   @SafeHtml
318   private String company;
319   @Column(name = "department_name", length = 200)
320   @Size(max = 200)
321   @SafeHtml
322   private String departmentName;
323   @Column(name = "job_title", length = 100)
324   @Size(max = 100)
325   @SafeHtml
326   private String jobTitle;
327   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
328   @JoinColumn(name = "timezone")
329   private FnLuTimezone timezone;
330   @Column(name = "department", length = 25)
331   @Size(max = 25)
332   @SafeHtml
333   private String department;
334   @Column(name = "business_unit", length = 25)
335   @Size(max = 25)
336   @SafeHtml
337   private String businessUnit;
338   @Column(name = "business_unit_name", length = 100)
339   @Size(max = 100)
340   @SafeHtml
341   private String businessUnitName;
342   @Column(name = "cost_center", length = 25)
343   @Size(max = 25)
344   @SafeHtml
345   private String cost_center;
346   @Column(name = "fin_loc_code", length = 10)
347   @Size(max = 10)
348   @SafeHtml
349   private String finLocCode;
350   @Column(name = "silo_status", length = 10)
351   @Size(max = 10)
352   @SafeHtml
353   private String siloStatus;
354   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
355   @JoinColumn(name = "language_id", nullable = false, columnDefinition = "bigint DEFAULT 1")
356   @NotNull(message = "languageId must not be null")
357   private FnLanguage languageId;
358   @Column(name = "is_guest", nullable = false, columnDefinition = "bit DEFAULT 0")
359   @NotNull(message = "guest must not be null")
360   private Boolean guest;
361   @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "fnUserList")
362   private Set<CrReportFileHistory> crReportFileHistorie;
363   @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
364   private Set<FnRole> fnRoles;
365   @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
366   private Set<FnMenuFunctional> fnRoleList;
367   @OneToMany(
368       targetEntity = FnAuditLog.class,
369       mappedBy = "userId",
370       cascade = CascadeType.ALL,
371       fetch = FetchType.LAZY
372   )
373   private Set<FnAuditLog> fnAuditLogs;
374   @OneToMany(
375       targetEntity = FnUser.class,
376       mappedBy = "createdId",
377       cascade = CascadeType.ALL,
378       fetch = FetchType.LAZY
379   )
380   private Set<FnUser> fnUsersCreatedId;
381   @OneToMany(
382       targetEntity = FnUser.class,
383       mappedBy = "managerId",
384       cascade = CascadeType.ALL,
385       fetch = FetchType.LAZY
386   )
387   private Set<FnUser> fnUsersManagerId;
388   @OneToMany(
389       targetEntity = FnUser.class,
390       mappedBy = "modifiedId",
391       cascade = CascadeType.ALL,
392       fetch = FetchType.LAZY
393   )
394   private Set<FnUser> fnUsersModifiedId;
395   @OneToMany(
396       targetEntity = EpUserRolesRequest.class,
397       mappedBy = "userId",
398       cascade = CascadeType.ALL,
399       fetch = FetchType.LAZY
400   )
401   private Set<EpUserRolesRequest> epUserRolesRequests;
402   @OneToMany(
403       targetEntity = FnPersUserAppSel.class,
404       mappedBy = "userId",
405       cascade = CascadeType.ALL,
406       fetch = FetchType.LAZY
407   )
408   private Set<FnPersUserAppSel> persUserAppSels;
409   @OneToMany(
410       targetEntity = EpWidgetCatalogParameter.class,
411       mappedBy = "userId",
412       cascade = CascadeType.ALL,
413       fetch = FetchType.LAZY
414   )
415   private Set<EpWidgetCatalogParameter> epWidgetCatalogParameters;
416   @OneToMany(
417       targetEntity = EpPersUserWidgetPlacement.class,
418       mappedBy = "userId",
419       cascade = CascadeType.ALL,
420       fetch = FetchType.LAZY
421   )
422   private Set<EpPersUserWidgetPlacement> epPersUserWidgetPlacements;
423   @OneToMany(
424       targetEntity = EpPersUserWidgetSel.class,
425       mappedBy = "userId",
426       cascade = CascadeType.ALL,
427       fetch = FetchType.LAZY
428   )
429   private Set<EpPersUserWidgetSel> epPersUserWidgetSels;
430   @OneToMany(
431       targetEntity = FnUserRole.class,
432       mappedBy = "userId",
433       cascade = CascadeType.ALL,
434       fetch = FetchType.LAZY
435   )
436   private Set<FnUserRole> userApps;
437   @OneToMany(
438       targetEntity = EpUserNotification.class,
439       mappedBy = "userId",
440       cascade = CascadeType.ALL,
441       fetch = FetchType.LAZY
442   )
443   private Set<EpUserNotification> epUserNotifications;
444
445   @Override
446   public Collection<? extends GrantedAuthority> getAuthorities() {
447     return fnRoles
448         .stream()
449         .map(fnRole -> new SimpleGrantedAuthority("ROLE_" + fnRole.getRoleName()))
450         .collect(Collectors.toList());
451   }
452
453   @Override
454   public String getPassword() {
455     return this.getLoginPwd();
456   }
457
458   @Override
459   public String getUsername() {
460     return this.getLoginId();
461   }
462
463   @Override
464   public boolean isAccountNonExpired() {
465     return true;
466   }
467
468   @Override
469   public boolean isAccountNonLocked() {
470     return true;
471   }
472
473   @Override
474   public boolean isCredentialsNonExpired() {
475     return true;
476   }
477
478   @Override
479   public boolean isEnabled() {
480     return true;
481   }
482
483   public String getFullName() {
484     return this.firstName + " " + this.lastName;
485   }
486
487   public SortedSet<FnRole> getAppEPRoles(FnApp app) {
488
489     logger.debug(EELFLoggerDelegate.debugLogger, "In EPUser.getAppEPRoles() - app = {}", app.getAppName());
490
491     SortedSet<FnRole> roles = new TreeSet<>();
492     Set<FnUserRole> userAppRoles = getUserApps();
493
494     logger.debug(EELFLoggerDelegate.debugLogger, "In EPUser.getAppEPRoles() - userApps = {} ", userAppRoles.size());
495
496     Iterator<FnUserRole> userAppRolesIterator = userAppRoles.iterator();
497
498     FnUserRole userAppRole;
499     // getting default app
500     while (userAppRolesIterator.hasNext()) {
501       FnUserRole tempUserApp = userAppRolesIterator.next();
502       if (tempUserApp.getAppId().getId().equals(app.getId())) {
503
504         logger.debug(EELFLoggerDelegate.debugLogger,
505             "In EPUser.getAppEPRoles() - for user {}, found application {}", this.getFullName(),
506             app.getAppName());
507
508         userAppRole = tempUserApp;
509
510         FnRole role = userAppRole.getRoleId();
511         if (role.getActiveYn()) {
512           logger.debug(EELFLoggerDelegate.debugLogger,
513               "In EPUser.getAppEPRoles() - Role {} is active - adding for user {} and app {}",
514               role.getRoleName(), this.getFullName(), app.getAppName());
515           roles.add(role);
516         } else {
517           logger.debug(EELFLoggerDelegate.debugLogger,
518               "In EPUser.getAppEPRoles() - Role {} is NOT active - NOT adding for user {} and app {}",
519               role.getRoleName(), this.getFullName(), app.getAppName());
520         }
521       }
522     }
523     logger.debug(EELFLoggerDelegate.debugLogger, "In EPUser.getAppEPRoles() - roles = {}", roles.size());
524
525     return roles;
526   }
527
528   public void setRoles(Set<FnRole> roles) {
529     FnApp app = new FnApp();
530     app.setId(1L);
531     app.setAppName("Default");
532     this.addAppRoles(app, roles);
533   }
534
535   private void addAppRoles(FnApp app, Set<FnRole> roles) {
536     if (roles != null) {
537       Set<FnUserRole> newUserApps = new HashSet<>();
538       for (FnRole role : roles) {
539         FnUserRole userApp = new FnUserRole();
540         userApp.setUserId(this);
541         userApp.setAppId(app);
542         userApp.setRoleId(role);
543         newUserApps.add(userApp);
544       }
545
546       this.setUserApps(newUserApps);
547     } else {
548       this.userApps.clear();
549     }
550
551   }
552 }