688 lines
18 KiB
Java
688 lines
18 KiB
Java
package com.midm.model.org;
|
|
|
|
import com.google.api.client.util.Strings;
|
|
import com.google.gson.annotations.Expose;
|
|
import com.midm.constant.LocaleConst;
|
|
import com.midm.model.GeneralProfile;
|
|
import com.midm.model.system.PolicyAgreement;
|
|
import com.mitac.cell.db.DbObject;
|
|
import com.mitac.cell.utils.McEmailUtils;
|
|
import com.mitac.cell.utils.McStringUtils;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
import org.springframework.data.annotation.Transient;
|
|
import org.springframework.data.mongodb.core.index.Indexed;
|
|
import org.springframework.data.mongodb.core.mapping.Document;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
@Document
|
|
public class User extends GeneralProfile {
|
|
public static final int MAX_LOGIN_ATTEMPT = 5;
|
|
public static final int LOGIN_AFTER_MINUTES = 10;
|
|
@Indexed
|
|
private UUID siteUuid;
|
|
@Indexed
|
|
private String email;
|
|
@Expose(
|
|
serialize = false
|
|
)
|
|
private String password;
|
|
private List sites;
|
|
@Transient
|
|
private boolean isMultiSiteUser;
|
|
@Transient
|
|
private boolean isApiUser;
|
|
@Transient
|
|
private SiteInfo siteInfo;
|
|
private Set systemRoles;
|
|
@Transient
|
|
private UUID groupUuid;
|
|
protected Date activated;
|
|
@Transient
|
|
private Site site;
|
|
@Transient
|
|
private UserGroup userGroup;
|
|
|
|
public User() {
|
|
}
|
|
|
|
public User(UUID userUuid) {
|
|
this.setUuid(userUuid);
|
|
}
|
|
|
|
public User(String email) {
|
|
this();
|
|
this.setEmail(email);
|
|
}
|
|
|
|
public User(String email, String password) {
|
|
this(email);
|
|
this.setPassword(password);
|
|
}
|
|
|
|
public void init() {
|
|
super.init();
|
|
if (this.hasRole("SUPERVISOR") || this.hasSystemRole("SYSTEM_ADMIN")) {
|
|
this.setEnabled(true);
|
|
}
|
|
|
|
this.setActivated();
|
|
if (Strings.isNullOrEmpty(super.getName())) {
|
|
this.setName(this.getName());
|
|
}
|
|
|
|
}
|
|
|
|
public UUID getSiteUuid() {
|
|
if (this.siteUuid == null && !this.hasSystemRole()) {
|
|
List<SiteInfo> sites = this.getSites();
|
|
if (!sites.isEmpty()) {
|
|
this.setSiteUuid(((SiteInfo)sites.get(0)).getSiteUuid());
|
|
}
|
|
}
|
|
|
|
return this.siteUuid;
|
|
}
|
|
|
|
public void setSiteUuid(UUID siteUuid) {
|
|
this.siteUuid = siteUuid;
|
|
SiteInfo siteInfo = this.getSiteInfo(siteUuid);
|
|
if (siteInfo != null) {
|
|
this.setRoles(siteInfo.getRoles());
|
|
}
|
|
|
|
}
|
|
|
|
public Site getSite() {
|
|
return this.site;
|
|
}
|
|
|
|
public void setSite(Site site) {
|
|
this.site = site;
|
|
}
|
|
|
|
public boolean isReseller() {
|
|
return this.site != null && this.site.isReseller();
|
|
}
|
|
|
|
public boolean isLite() {
|
|
return this.site != null && this.site.isLite();
|
|
}
|
|
|
|
public boolean isStandard() {
|
|
return this.site != null && this.site.isStandard();
|
|
}
|
|
|
|
public boolean isStandardWithBatteryPro() {
|
|
return this.site != null && this.site.isStandardWithBatteryPro();
|
|
}
|
|
|
|
public boolean isFeatureAllowed(String subCategory, String item) {
|
|
return this.isLite() ? this.site != null && this.site.isFeatureAllowed(subCategory, item) : true;
|
|
}
|
|
|
|
public boolean isFilesFeatureAllowed(String category, String key) {
|
|
return this.isFeatureAllowed(category, key) && (this.hasRole("SUPERVISOR") || this.hasRole("PROFILE_MANAGER"));
|
|
}
|
|
|
|
public String getEmail() {
|
|
return this.email;
|
|
}
|
|
|
|
public void setEmail(String email) {
|
|
if (!Strings.isNullOrEmpty(email)) {
|
|
if (McEmailUtils.isValid(email)) {
|
|
this.email = email.toLowerCase();
|
|
}
|
|
|
|
if (Strings.isNullOrEmpty(super.getName())) {
|
|
this.setName(this.getName());
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public String getPassword() {
|
|
return this.password;
|
|
}
|
|
|
|
public boolean setPassword(String password) {
|
|
this.password = password;
|
|
return !Strings.isNullOrEmpty(password) && password.length() >= 8 ? McStringUtils.hasUpperLowerDigit(password) : false;
|
|
}
|
|
|
|
public String getName() {
|
|
String name = super.getName();
|
|
if (Strings.isNullOrEmpty(name)) {
|
|
name = this.getEmail().substring(0, this.getEmail().indexOf("@"));
|
|
}
|
|
|
|
return name;
|
|
}
|
|
|
|
public void setSiteInfo(SiteInfo siteInfo) {
|
|
this.siteInfo = siteInfo;
|
|
}
|
|
|
|
public List getSites() {
|
|
if (this.sites == null) {
|
|
this.sites = new ArrayList();
|
|
}
|
|
|
|
return this.sites;
|
|
}
|
|
|
|
public List getSiteUuids() {
|
|
return this.sites != null && !this.sites.isEmpty() ? (List)this.sites.stream().map((item) -> item.getSiteUuid()).collect(Collectors.toList()) : null;
|
|
}
|
|
|
|
public boolean clearUndefinedSites() {
|
|
boolean hasChanges = false;
|
|
if (this.sites != null) {
|
|
Iterator<SiteInfo> it = this.sites.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
SiteInfo site = (SiteInfo)it.next();
|
|
if (site.getSiteUuid() == null) {
|
|
it.remove();
|
|
hasChanges = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return hasChanges;
|
|
}
|
|
|
|
public void setSites(List sites) {
|
|
this.sites = sites;
|
|
}
|
|
|
|
public boolean isMultiSiteUser() {
|
|
return this.isMultiSiteUser;
|
|
}
|
|
|
|
public void setMultiSiteUser() {
|
|
this.isMultiSiteUser = this.getSites().size() > 1;
|
|
if (!this.isMultiSiteUser) {
|
|
this.isApiUser = this.isApiUser();
|
|
}
|
|
|
|
}
|
|
|
|
public SiteInfo getSiteInfo() {
|
|
return this.getSiteInfo(this.getSiteUuid());
|
|
}
|
|
|
|
public SiteInfo getSiteInfo(Site site) {
|
|
return this.getSiteInfo(site.getUuid());
|
|
}
|
|
|
|
public SiteInfo getSiteInfo(UUID siteUuid) {
|
|
if (siteUuid == null) {
|
|
return null;
|
|
} else {
|
|
for(SiteInfo siteInfo : this.getSites()) {
|
|
if (siteInfo.getSiteUuid() != null && siteInfo.getSiteUuid().equals(siteUuid)) {
|
|
return siteInfo;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public boolean isBelongedToSite(Site site) {
|
|
return this.isBelongedToSite(site.getUuid());
|
|
}
|
|
|
|
public boolean isBelongedToSite(UUID siteUuid) {
|
|
for(SiteInfo siteInfo : this.getSites()) {
|
|
if (siteInfo.getSiteUuid() != null && siteInfo.getSiteUuid().equals(siteUuid)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public boolean canHandle(UUID siteUuid, String... roles) {
|
|
SiteInfo info = this.getSiteInfo(siteUuid);
|
|
if (info != null) {
|
|
if (info.hasRole("SUPERVISOR")) {
|
|
return true;
|
|
}
|
|
|
|
for(String role : roles) {
|
|
if (info.hasRole(role)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public SiteInfo addSite(UUID siteUuid) {
|
|
if (siteUuid == null) {
|
|
return null;
|
|
} else {
|
|
if (!this.isBelongedToSite(siteUuid)) {
|
|
this.getSites().add(new SiteInfo(siteUuid));
|
|
}
|
|
|
|
if (this.getSiteUuid() == null) {
|
|
this.setSiteUuid(siteUuid);
|
|
}
|
|
|
|
SiteInfo siteInfo = this.getSiteInfo(siteUuid);
|
|
siteInfo.setEnabled(true);
|
|
return siteInfo;
|
|
}
|
|
}
|
|
|
|
public void removeSite(UUID siteUuid) {
|
|
if (siteUuid != null) {
|
|
SiteInfo existSite = null;
|
|
|
|
for(SiteInfo siteInfo : this.getSites()) {
|
|
if (siteInfo.getSiteUuid().equals(siteUuid)) {
|
|
existSite = siteInfo;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (existSite != null) {
|
|
this.getSites().remove(existSite);
|
|
}
|
|
|
|
if (this.getSites().isEmpty()) {
|
|
this.setSiteUuid((UUID)null);
|
|
this.setGroupUuid((UUID)null);
|
|
this.setLastSite((UUID)null);
|
|
} else {
|
|
if (siteUuid.equals(this.getSiteUuid())) {
|
|
this.setSiteUuid((UUID)null);
|
|
this.setGroupUuid((UUID)null);
|
|
}
|
|
|
|
if (siteUuid.equals(this.getLastSite())) {
|
|
this.setLastSite((UUID)null);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public Set getRoles() {
|
|
return this.getRolesBySite(this.getSiteUuid());
|
|
}
|
|
|
|
public Set getRoles(UUID siteUuid) {
|
|
return this.getRolesBySite(siteUuid);
|
|
}
|
|
|
|
public Set getRolesBySite(UUID siteUuid) {
|
|
SiteInfo info = this.getSiteInfo(siteUuid);
|
|
return (Set)(info != null ? this.getSiteInfo(siteUuid).getRoles() : new HashSet());
|
|
}
|
|
|
|
public void setRoles(Set roles) {
|
|
SiteInfo info = this.getSiteInfo(this.getSiteUuid());
|
|
if (info != null) {
|
|
this.getSiteInfo(this.getSiteUuid()).setRoles(roles);
|
|
}
|
|
|
|
}
|
|
|
|
public void addRoleToSite(String role, UUID siteUuid) {
|
|
role = UserRole.toRole(role);
|
|
if (role != null) {
|
|
if (role.equals("SUPERVISOR")) {
|
|
this.getRoles(siteUuid).clear();
|
|
}
|
|
|
|
this.getRolesBySite(siteUuid).add(role);
|
|
}
|
|
|
|
}
|
|
|
|
public void removeRole(String role) {
|
|
this.getRoles().remove(role);
|
|
}
|
|
|
|
public void removeRole(UUID siteUuid, String role) {
|
|
SiteInfo siteInfo = this.getSiteInfo(siteUuid);
|
|
if (siteInfo != null) {
|
|
siteInfo.getRoles().remove(role);
|
|
}
|
|
|
|
}
|
|
|
|
public Set getSystemRoles() {
|
|
if (this.systemRoles == null) {
|
|
this.systemRoles = new HashSet();
|
|
}
|
|
|
|
return this.systemRoles;
|
|
}
|
|
|
|
public void setSystemRoles(Set systemRoles) {
|
|
this.systemRoles = systemRoles;
|
|
}
|
|
|
|
public void addSystemRole(String role) {
|
|
this.getSystemRoles().add(role);
|
|
}
|
|
|
|
public void removeSystemRole(String role) {
|
|
this.getSystemRoles().remove(role);
|
|
}
|
|
|
|
public boolean hasSystemRole() {
|
|
return !this.getSystemRoles().isEmpty();
|
|
}
|
|
|
|
public boolean hasSystemRole(String role) {
|
|
return this.getSystemRoles().contains(role);
|
|
}
|
|
|
|
public boolean hasRole(String role) {
|
|
return this.hasRole(this.getSiteUuid(), role);
|
|
}
|
|
|
|
public boolean hasRole(UUID siteUuid, String role) {
|
|
SiteInfo site = this.getSiteInfo(siteUuid);
|
|
return site != null ? site.hasRole(role) : false;
|
|
}
|
|
|
|
public boolean isSupervisor() {
|
|
return this.hasRole("SUPERVISOR");
|
|
}
|
|
|
|
public boolean isSystemUser() {
|
|
return !this.getSystemRoles().isEmpty();
|
|
}
|
|
|
|
public boolean isSystemAdmin() {
|
|
return this.getSystemRoles().contains("SYSTEM_ADMIN");
|
|
}
|
|
|
|
public UUID getGroupUuid() {
|
|
return this.groupUuid;
|
|
}
|
|
|
|
public void setGroupUuid(UUID groupUuid) {
|
|
this.groupUuid = groupUuid;
|
|
}
|
|
|
|
public Date getActivated() {
|
|
return this.activated;
|
|
}
|
|
|
|
public void setActivated(Date activated) {
|
|
this.activated = activated;
|
|
}
|
|
|
|
public void setActivated() {
|
|
this.activated = new Date();
|
|
this.enabled = true;
|
|
}
|
|
|
|
public boolean isActivated() {
|
|
return true;
|
|
}
|
|
|
|
public boolean isAvailable() {
|
|
return this.getEnabled() && this.isActivated();
|
|
}
|
|
|
|
public String getEmailAddress() {
|
|
String EMAIL_FORMAT = "%s<%s>";
|
|
String name = this.getName();
|
|
if (McStringUtils.isNullOrEmpty(this.email)) {
|
|
return null;
|
|
} else {
|
|
return McStringUtils.isNullOrEmpty(name) ? this.email : String.format("%s<%s>", name, this.email);
|
|
}
|
|
}
|
|
|
|
public UserGroup getUserGroup() {
|
|
return this.userGroup;
|
|
}
|
|
|
|
public void setUserGroup(UserGroup userGroup) {
|
|
this.userGroup = userGroup;
|
|
if (userGroup != null) {
|
|
SiteInfo siteInfo = this.getSiteInfo(userGroup.getSiteUuid());
|
|
if (siteInfo != null) {
|
|
siteInfo.setGroupUuid(userGroup.getUuid());
|
|
} else {
|
|
this.userGroup = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void encodePassword(PasswordEncoder encoder, String password) {
|
|
this.password = encoder.encode(password);
|
|
}
|
|
|
|
public void encodePassword(PasswordEncoder encoder) {
|
|
this.password = encoder.encode(this.password);
|
|
}
|
|
|
|
public boolean canRegister() {
|
|
return !McStringUtils.isNullOrEmpty(this.getEmail()) && !McStringUtils.isNullOrEmpty(this.getPassword());
|
|
}
|
|
|
|
public List getAuthorities() {
|
|
return UserRole.getAuthorities(this.getRoles());
|
|
}
|
|
|
|
public String getTheme() {
|
|
String theme = "default";
|
|
if (this.site != null) {
|
|
String model = this.site.getModel();
|
|
if (!Strings.isNullOrEmpty(model) && !model.equals("midm")) {
|
|
theme = model;
|
|
}
|
|
}
|
|
|
|
return theme;
|
|
}
|
|
|
|
public String getLocale() {
|
|
String locale = this.getSettingAsString("language");
|
|
return McStringUtils.isNullOrEmpty(locale) ? LocaleConst.DEFAULT_LOCALE.toLanguageTag() : locale;
|
|
}
|
|
|
|
public void setLocale(Locale locale) {
|
|
this.setLocale(locale.toLanguageTag());
|
|
}
|
|
|
|
public void setLocale(String locale) {
|
|
this.setSetting("language", locale);
|
|
}
|
|
|
|
public boolean isSameLocale(Locale locale) {
|
|
return this.isSameLocale(locale.toLanguageTag());
|
|
}
|
|
|
|
public boolean isSameLocale(String localeTag) {
|
|
return this.getLocale().equals(localeTag);
|
|
}
|
|
|
|
public boolean keepSignIn() {
|
|
return this.getSettingAsBoolean("keepSignIn");
|
|
}
|
|
|
|
public UUID getLastSite() {
|
|
UUID uuid = this.getSettingAsUuid("lastSite");
|
|
if (uuid == null && this.sites != null && !this.sites.isEmpty() && !this.hasSystemRole()) {
|
|
uuid = ((SiteInfo)this.sites.get(0)).getSiteUuid();
|
|
this.setLastSite(uuid);
|
|
}
|
|
|
|
return uuid;
|
|
}
|
|
|
|
public void setLastSite(UUID uuid) {
|
|
this.setSetting("lastSite", uuid);
|
|
}
|
|
|
|
public String getLastPage() {
|
|
SiteInfo siteInfo = this.getSiteInfo(this.siteUuid);
|
|
return siteInfo == null ? this.getSettingAsString("lastPage") : siteInfo.getSettingAsString("lastPage");
|
|
}
|
|
|
|
public List getDashboardSettings() {
|
|
SiteInfo siteInfo = this.getSiteInfo(this.siteUuid);
|
|
if (siteInfo != null) {
|
|
Object settings = siteInfo.getSetting("dashboard");
|
|
if (settings != null && settings instanceof List) {
|
|
return (List)settings;
|
|
}
|
|
}
|
|
|
|
return new ArrayList();
|
|
}
|
|
|
|
public DbObject getReportSettings() {
|
|
SiteInfo siteInfo = this.getSiteInfo(this.siteUuid);
|
|
return siteInfo != null ? siteInfo.getSettings().getDbObject(new String[]{"report"}) : new DbObject();
|
|
}
|
|
|
|
public void setDashboardSettings(List dashboard) {
|
|
SiteInfo siteInfo = this.getSiteInfo(this.siteUuid);
|
|
if (siteInfo != null) {
|
|
siteInfo.setSetting("dashboard", dashboard);
|
|
}
|
|
|
|
}
|
|
|
|
public void setLastPage(String url) {
|
|
SiteInfo siteInfo = this.getSiteInfo(this.siteUuid);
|
|
if (siteInfo != null) {
|
|
siteInfo.setSetting("lastPage", url);
|
|
}
|
|
|
|
}
|
|
|
|
public DbObject getPageSettings(String pageUrl) {
|
|
Object settings = this.getSetting("page", pageUrl);
|
|
DbObject var3;
|
|
if (settings == null) {
|
|
var3 = new DbObject();
|
|
} else {
|
|
if (settings instanceof DbObject) {
|
|
return (DbObject)settings;
|
|
}
|
|
|
|
if (settings instanceof Map) {
|
|
var3 = new DbObject((Map)settings);
|
|
} else {
|
|
var3 = new DbObject();
|
|
}
|
|
}
|
|
|
|
this.setSetting("page", pageUrl, var3);
|
|
return var3;
|
|
}
|
|
|
|
public Object getPageSetting(String pageUrl, String key) {
|
|
return this.getPageSettings(pageUrl).get(key);
|
|
}
|
|
|
|
public void setPageSetting(String pageUrl, String key, Object value) {
|
|
if (value == null) {
|
|
this.getPageSettings(pageUrl).remove(key);
|
|
} else {
|
|
this.getPageSettings(pageUrl).put(key, value);
|
|
}
|
|
|
|
}
|
|
|
|
public void updateProfile(DbObject newProfile) {
|
|
DbObject profile = this.getProfile();
|
|
newProfile.forEach((key, value) -> {
|
|
if (!key.equals("eula") && !key.equals("ip")) {
|
|
profile.put(key, value);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
public void setEula(PolicyAgreement agreement) {
|
|
this.setProfile("eula", (new DbObject("uuid", agreement.getUuid().toString())).append("version", agreement.getVersion()).append("acceptDate", new Date()));
|
|
}
|
|
|
|
public void setEula(String value) {
|
|
Object object = McStringUtils.isNullOrEmpty(value) ? new DbObject() : DbObject.parse(value);
|
|
this.setProfile("eula", object);
|
|
}
|
|
|
|
public String getMfaMethod() {
|
|
return this.getSettingAsString("mfa");
|
|
}
|
|
|
|
public boolean isEulaAgreed() {
|
|
return this.isApiUser() || this.getCategoryProfile("eula", "acceptDate") != null;
|
|
}
|
|
|
|
public boolean isApiUser() {
|
|
SiteInfo siteInfo = this.getSiteInfo();
|
|
return siteInfo != null && siteInfo.isApiOnly();
|
|
}
|
|
|
|
public boolean equals(Object user) {
|
|
if (user == null) {
|
|
return false;
|
|
} else if (user instanceof User) {
|
|
return this.getEmail().equalsIgnoreCase(((User)user).getEmail());
|
|
} else if (user instanceof String) {
|
|
return this.getEmail().equals((String)user);
|
|
} else {
|
|
return user instanceof UUID ? user.equals(this.getUuid()) : super.equals(user);
|
|
}
|
|
}
|
|
|
|
public void setProfileOnly() {
|
|
this.siteUuid = null;
|
|
this.sites = null;
|
|
this.systemRoles = null;
|
|
this.activated = null;
|
|
this.settings = null;
|
|
this.created = null;
|
|
this.updated = null;
|
|
this.getProfile().remove("eula");
|
|
}
|
|
|
|
public DbObject getLoginAttempt() {
|
|
return this.getSettings().getDbObject(new String[]{"loginAttempt"});
|
|
}
|
|
|
|
public void loginFailed() {
|
|
DbObject loginAttempt = this.getLoginAttempt();
|
|
loginAttempt.put("attemptAt", new Date());
|
|
int count = loginAttempt.getInteger("count", 0) + 1;
|
|
loginAttempt.put("count", count % 6);
|
|
this.setSetting("loginAttempt", loginAttempt);
|
|
}
|
|
|
|
public void resetLoginAttempt() {
|
|
this.getSettings().remove("loginAttempt");
|
|
}
|
|
|
|
public String toString() {
|
|
return this.getEmail();
|
|
}
|
|
}
|