Commit 8341669a authored by Matija Obreza's avatar Matija Obreza
Browse files

Moved AuthUserDetailsService to service/impl

parent 49d1d355
......@@ -14,8 +14,7 @@
* limitations under the License.
**/
package org.genesys2.server.security;
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.Collection;
......@@ -23,6 +22,7 @@ import java.util.List;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
......@@ -30,48 +30,48 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
public class AuthUserDetailsService implements UserDetailsService{
@Component("authUserDetailsService")
public class AuthUserDetailsService implements UserDetailsService {
@Autowired
private UserService userService;
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userService.getUserByEmail(email);
User user = userService.getUserByEmail(email);
if (user == null){
return null;
}
if (user == null) {
return null;
}
AuthUserDetails userDetails = new AuthUserDetails(
email, user.getPassword(), getGrantedAuthorities(user)
);
AuthUserDetails userDetails = new AuthUserDetails(email, user.getPassword(), getGrantedAuthorities(user));
//set actual DB user for possible further purposes
userDetails.setUser(user);
// set actual DB user for possible further purposes
userDetails.setUser(user);
return userDetails;
}
return userDetails;
}
private Collection<? extends GrantedAuthority> getGrantedAuthorities(User user) {
List<UserRole> userRoles = new ArrayList<UserRole>(UserRole.values().length);
private Collection<? extends GrantedAuthority> getGrantedAuthorities(User user) {
List<UserRole> userRoles = new ArrayList<UserRole>(UserRole.values().length);
//consider, that every user has USER role
userRoles.add(UserRole.USER);
// consider, that every user has USER role
userRoles.add(UserRole.USER);
for(UserRole userRole: user.getRoles()){
if (UserRole.ADMINISTRATOR.equals(userRole)){
userRoles.add(UserRole.ADMINISTRATOR);
}
}
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
for (UserRole userRole : user.getRoles()) {
if (UserRole.ADMINISTRATOR.equals(userRole)) {
userRoles.add(UserRole.ADMINISTRATOR);
}
}
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
for (UserRole authority: userRoles){
grantedAuthorities.add(new SimpleGrantedAuthority(authority.name()));
}
for (UserRole authority : userRoles) {
grantedAuthorities.add(new SimpleGrantedAuthority(authority.name()));
}
return grantedAuthorities;
}
return grantedAuthorities;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment