Zapomenuté soubory
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package info.bukova.isspst;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public final static String DEF_ADMIN = "admin";
|
||||
public final static String DEF_ADMIN_PASSWD = "admin";
|
||||
public final static String ROLE_USER = "ROLE_USER";
|
||||
public final static String ROLE_ADMIN = "ROLE_ADMIN";
|
||||
public final static String ROLES[] = {ROLE_USER, ROLE_ADMIN};
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package info.bukova.isspst;
|
||||
|
||||
import info.bukova.isspst.data.Role;
|
||||
import info.bukova.isspst.data.User;
|
||||
import info.bukova.isspst.services.RoleService;
|
||||
import info.bukova.isspst.services.UserService;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
public class DbInitListener implements ServletContextListener {
|
||||
|
||||
private RoleService roleService;
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent arg0) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent evt) {
|
||||
Logger logger = LoggerFactory.getLogger(DbInitListener.class);
|
||||
logger.info("Initializing database");
|
||||
|
||||
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(evt.getServletContext());
|
||||
roleService = ctx.getBean(RoleService.class);
|
||||
userService = ctx.getBean(UserService.class);
|
||||
|
||||
checkRoles();
|
||||
checkUsers();
|
||||
}
|
||||
|
||||
private void checkRoles() {
|
||||
Role r = null;
|
||||
|
||||
for (String auth : Constants.ROLES)
|
||||
{
|
||||
r = roleService.getRoleByAuthority(auth);
|
||||
if (r == null) {
|
||||
r = new Role();
|
||||
r.setAuthority(auth);
|
||||
r.setDescription("---");
|
||||
roleService.add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkUsers() {
|
||||
boolean adminExists = false;
|
||||
|
||||
for (User u : userService.getAll()) {
|
||||
if (userService.hasRole(u, Constants.ROLE_ADMIN)) {
|
||||
adminExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!adminExists) {
|
||||
User u = new User();
|
||||
u.setUsername(Constants.DEF_ADMIN);
|
||||
u.addAuthority(roleService.getRoleByAuthority(Constants.ROLE_ADMIN));
|
||||
u.setEnabled(true);
|
||||
userService.setPassword(u, Constants.DEF_ADMIN_PASSWD);
|
||||
userService.add(u);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package info.bukova.isspst;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
|
||||
public class LoginFailHandler implements AuthenticationFailureHandler {
|
||||
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex)
|
||||
throws IOException, ServletException {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(LoginFailHandler.class);
|
||||
logger.info("Login failed with message: " + ex.getMessage());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,6 +36,7 @@ public class User implements UserDetails, DataModel {
|
||||
private String password;
|
||||
@Column(name="ENABLED")
|
||||
private boolean enabled;
|
||||
private String fullName;
|
||||
@OneToMany(fetch=FetchType.EAGER) @JoinTable(name="USER_ROLE")
|
||||
@MapKeyColumn(name="ROLE_ID")
|
||||
private List<Role> authorities;
|
||||
@@ -137,4 +138,12 @@ public class User implements UserDetails, DataModel {
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
public void setFullName(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ListViewModel<T extends DataModel> {
|
||||
public List<T> getDataList() {
|
||||
if (dataList == null) {
|
||||
dataList = new ArrayList<T>();
|
||||
loadFromDb();
|
||||
loadFromDbSync();
|
||||
}
|
||||
|
||||
return dataList;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package info.bukova.isspst.ui;
|
||||
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
|
||||
public class UserForm extends FormViewModel<User> {
|
||||
|
||||
@Init(superclass = true)
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package info.bukova.isspst.ui;
|
||||
|
||||
import org.zkoss.bind.annotation.Init;
|
||||
import org.zkoss.zk.ui.select.annotation.WireVariable;
|
||||
|
||||
import info.bukova.isspst.data.User;
|
||||
import info.bukova.isspst.services.UserService;
|
||||
|
||||
public class UsersList extends ListViewModel<User> {
|
||||
|
||||
@WireVariable
|
||||
private UserService userService;
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
service = userService;
|
||||
dataClass = User.class;
|
||||
formZul = "userForm.zul";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user