Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7913790 to your computer and use it in GitHub Desktop.
Save anonymous/7913790 to your computer and use it in GitHub Desktop.
package eu.shishigami.school.web.controller.admin.user;
@Component
@Scope(value = "session")
@Slf4j
public class AddUserController {
@Autowired
private AddUserView addUserView;
@Autowired
private UserService userService;
@Autowired
private GroupService groupService;
@PostConstruct
public void init() {
addUserView.setUserEntity(new UserEntity());
addUserView.setAllGroups(groupService.findAll());
LoggingUtil.logInitialization(log);
}
public void handleSave() {
// BCrypt password encoding..
addUserView.setUserEntity(userService.save(addUserView.getUserEntity()));
LoggingUtil.logSave(log);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<ui:composition>
<f:event listener="#{addUserController.init}" type="preRenderView" />
<h:form id="addUserForm">
<p:inputText value="#{addUserView.userEntity.username}" id="usernameInput" placeholder="Username" />
<p:password value="#{addUserView.password}" id="passwordInput" placeholder="Password" />
<h:selectOneMenu value="#{addUserView.userEntity.group}" id="groupInput" converter="#{groupConverter}">
<f:selectItem itemValue="#{null}" itemLabel="None" />
<f:selectItems value="#{addUserView.allGroups}" var="group" itemValue="#{group}" itemLabel="#{group.groupName}" />
</h:selectOneMenu>
<!-- Mehr input Felder für andere Attribute.. -->
<p:commandButton actionListener="#{addUserController.handleSave}" oncomplete="if (!args.validationFailed) PF('addUserDlg').hide();" value="Save" update=":userAdminForm" />
</h:form>
</ui:composition>
</body>
</html>
package eu.shishigami.school.web.controller.admin.user;
@Component
@Scope(value = "session")
@Getter
@Setter
public class AddUserView {
private List<GroupEntity> allGroups;
private UserEntity userEntity;
private String password;
}
package eu.shishigami.school.domain;
@Entity
@Getter
@Setter
public class GroupEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@NotBlank
private String groupName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "group")
@LazyCollection(LazyCollectionOption.FALSE)
private List<UserEntity> users = new ArrayList<UserEntity>();
@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<RoleEntity> roles = new ArrayList<RoleEntity>();
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GroupEntity other = (GroupEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package eu.shishigami.school.domain;
@Entity
@Getter
@Setter
public class RoleEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@NotBlank
private String roleName;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RoleEntity other = (RoleEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package eu.shishigami.school.domain;
@Entity
@Getter
@Setter
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
@NotBlank
@Length(min = 4, max = 16)
private String username;
@NotBlank
@Length(min = 4, max = 128)
private String password;
@Email
@NotBlank
private String email;
@NotBlank
private String firstName;
@NotBlank
private String lastName;
private boolean enabled = true;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "group_id")
private GroupEntity group;
@Transient
public boolean hasRole(String roleName) {
for (RoleEntity role : group.getRoles()) {
if (role.getRoleName().equals(roleName)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserEntity other = (UserEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package eu.shishigami.school.repository;
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
public UserEntity findByUsername(String username);
}
package eu.shishigami.school.service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Transactional(readOnly = false)
public void delete(final UserEntity userEntity) {
userRepository.delete(userEntity);
}
@Transactional(readOnly = false)
public UserEntity save(final UserEntity userEntity) {
return userRepository.save(userEntity);
}
public UserEntity findByUsername(final String username) {
return userRepository.findByUsername(username);
}
public UserEntity findOne(final Long id) {
return userRepository.findOne(id);
}
public List<UserEntity> findAll() {
return userRepository.findAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment