1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| package fr.monpackage.web;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import fr.monpackage.domain.Users;
import fr.monpackage.metier.UsersService;
import fr.monpackage.metier.impl.FonctionService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
@Component
@Namespace("/")
public class WelcomeAction extends ActionSupport implements ModelDriven<Users> {
@Autowired
private UsersService usersService;
@Autowired
private FonctionService fonctionService;
private String username;
private Date myDate;
private String roleDev;
private String admin;
Users user = new Users();
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRoleDev() {
return roleDev;
}
public void setRoleDev(String roleDev) {
this.roleDev = roleDev;
}
public String getAdmin() {
return admin;
}
public void setAdmin(String admin) {
this.admin = admin;
}
public Date getMyDate() {
return myDate;
}
public void setMyDate(Date myDate) {
this.myDate = myDate;
}
@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
public String execute() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
ActionContext.getContext().getSession();
System.out.println("name : " + name);
return SUCCESS;
}
@Action(value = "login", results = { @Result(name = SUCCESS, location = "login.jsp") })
public String login() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
ActionContext.getContext().getSession();
return SUCCESS;
}
@Override
public Users getModel() {
return user;
}
@PreAuthorize(value="hasAnyRole('ROLE_DEV', 'ADMIN')")
@Action(value = "selectMenu", results = { @Result(name = SUCCESS, location = "search.jsp"), @Result(name=INPUT, location = "index.jsp")})
public String selectMenu() {
System.out.println(fonctionService.findAll().size());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
Collection collection = auth.getAuthorities();
for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println("AUTHORISSATION--->object = "+object);
}
Map<String, Object> session = ActionContext.getContext().getSession();
if(session.get("roleDev") != null){
roleDev = "true";
}else if(session.get("validateur") != null){
validateur = "true";
}
String langue = ActionContext.getContext().getLocale().getLanguage();
System.out.println("ActionContext.getContext().getApplication().values()-->langue = "+langue);
myDate = GregorianCalendar.getInstance().getTime();
System.out.println("@PreAuthorize-->name : " + name);
System.out.println("@PreAuthorize-->username : " + username);
System.out.println("@PreAuthorize-->roleDev : " + roleDev);
System.out.println("@PreAuthorize-->admin : " + admin);
if (roleDev != null && roleDev.equals("true"))
session.put("roleDev", true);
else if (admin != null && admin.equals("true"))
session.put("admin", true);
System.out.println(usersService);
Users user = usersService.findUsersesByUserNameEquals(username);
session.put("nomFamille", user.getNom());
session.put("prenom", user.getPrenom());
return SUCCESS;
}
} |
Partager