Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.model.core.auth

File User.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
36% of files have more coverage

Code metrics

10
34
17
1
156
131
22
0.65
2
17
1.29

Classes

Class Line # Actions
User 24 34 22
0.4590163845.9%
 

Contributing tests

This file is covered by 18 tests. .

Source view

1    package io.sunshower.model.core.auth;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.model.core.Schemata;
5    import io.sunshower.persistence.core.DistributableEntity;
6    import java.util.*;
7    import javax.persistence.*;
8    import javax.validation.constraints.NotNull;
9    import javax.validation.constraints.Size;
10    import javax.xml.bind.annotation.*;
11    import lombok.Getter;
12    import lombok.Setter;
13    import lombok.ToString;
14    import org.springframework.security.core.GrantedAuthority;
15    import org.springframework.security.core.userdetails.UserDetails;
16   
17    @Getter
18    @Setter
19    @Entity
20    @ToString
21    @XmlRootElement(name = "user")
22    @XmlAccessorType(XmlAccessType.NONE)
23    @Table(name = "PRINCIPAL", schema = Schemata.SUNSHOWER)
 
24    public class User extends DistributableEntity implements UserDetails, TenantAware {
25   
26    @NotNull
27    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
28    private Details details;
29   
30    @Basic
31    @XmlAttribute
32    @Column(unique = true)
33    private String username;
34   
35    @Basic
36    @XmlAttribute
37    @Size(min = 5, max = 255)
38    private String password;
39   
40    @Basic @XmlAttribute private boolean active;
41   
42    @XmlElement(name = "role")
43    @XmlElementWrapper(name = "roles")
44    @ManyToMany(cascade = CascadeType.ALL)
45    @JoinTable(
46    name = "users_to_roles",
47    schema = Schemata.SUNSHOWER,
48    joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
49    inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
50    )
51    private Set<Role> roles;
52   
53    @Getter
54    @ManyToOne
55    @JoinColumn(name = "tenant_id")
56    private Tenant tenant;
57   
 
58  17 toggle public User() {
59  17 super(DistributableEntity.sequence.next());
60    }
61   
 
62  0 toggle public User(User copy) {
63  0 this(copy.getId(), copy.getUsername(), null);
64    }
65   
 
66  1 toggle public User(Identifier id, final String username, final String password) {
67  1 super(id);
68  1 this.username = username;
69  1 this.password = password;
70    }
71   
 
72  0 toggle public User(Identifier uuid) {
73  0 super(uuid);
74    }
75   
 
76  5 toggle public Set<Role> getRoles() {
77  5 if (roles == null) {
78  2 return Collections.emptySet();
79    }
80  3 return roles;
81    }
82   
 
83  0 toggle @Override
84    public boolean isAccountNonExpired() {
85  0 return active;
86    }
87   
 
88  0 toggle @Override
89    public boolean isAccountNonLocked() {
90  0 return active;
91    }
92   
 
93  0 toggle @Override
94    public boolean isCredentialsNonExpired() {
95  0 return active;
96    }
97   
 
98  1 toggle @Override
99    public boolean isEnabled() {
100  1 return active;
101    }
102   
 
103  0 toggle @Override
104    public Collection<? extends GrantedAuthority> getAuthorities() {
105  0 return roles;
106    }
107   
 
108  18 toggle @Override
109    protected void setDefaults() {
110  18 this.setDetails(new Details(this));
111    }
112   
 
113  4 toggle public User addRole(Role role) {
114  4 role.addUser(this);
115  4 if (this.roles == null) {
116  4 this.roles = new LinkedHashSet<>();
117    }
118  4 this.roles.add(role);
119  4 return this;
120    }
121   
 
122  0 toggle public void setAuthorities(Collection<Role> roles) {
123  0 clearRoles();
124  0 if (roles != null) {
125  0 for (Role r : roles) {
126  0 addRole(r);
127    }
128    }
129    }
130   
 
131  0 toggle public void setRoles(Set<Role> roles) {
132  0 setAuthorities(roles);
133    }
134   
 
135  0 toggle public void removeRoles(Collection<Role> roles) {
136  0 for (Role role : roles) {
137  0 removeRole(role);
138    }
139    }
140   
 
141  0 toggle public void clearRoles() {
142  0 if (roles != null) {
143  0 Collection<Role> copy = new HashSet<>(roles);
144  0 for (Role r : copy) {
145  0 removeRole(r);
146    }
147    }
148    }
149   
 
150  1 toggle public void removeRole(Role role) {
151  1 if (this.roles != null) {
152  1 role.removeUser(this);
153  1 this.roles.remove(role);
154    }
155    }
156    }