Created
November 22, 2015 20:58
-
-
Save cortix/1f00542e19a409a2b3de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.berkadem.web.controller; | |
import org.apache.commons.lang3.text.WordUtils; | |
import org.berkadem.persistence.dao.ProjectsFlowRepository; | |
import org.berkadem.persistence.dao.ProjectsRepository; | |
import org.berkadem.persistence.dao.RoleRepository; | |
import org.berkadem.persistence.dao.UserRepository; | |
import org.berkadem.persistence.model.Privilege; | |
import org.berkadem.persistence.model.ProjectsTables; | |
import org.berkadem.persistence.model.Role; | |
import org.berkadem.persistence.model.User; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.Query; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
/** | |
* Created by HasanCelik on 21.05.2015. | |
*/ | |
@Controller | |
public class OrganisationController { | |
@Autowired | |
private UserRepository userRepository; | |
@Autowired | |
private RoleRepository roleRepository; | |
@Autowired | |
private ProjectsRepository projectsRepository; | |
@Autowired | |
private ProjectsFlowRepository projectsFlowRepository; | |
@PersistenceContext | |
private EntityManager em; | |
@RequestMapping(value = "/homepage" , method = RequestMethod.GET) | |
public String authenticatedUserInfo(final Model model, @RequestParam("user") final String user){ | |
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
if(auth.getName().equals(user)) { | |
final User userInfo=userRepository.findByEmail(auth.getName()); | |
Query projectsList = em.createNativeQuery("SELECT p.faaliyetTuru, p.projeNumarasi, p.projeSahibiKurum, p.projeAdi, p.ilgiliPersonel, pf.katSayisi, pf.refSayisi, pf.engKatSayisi, pf.perSayisi, p.projeBitTarihi, p.raporTarihi, SUM(pf.butce) AS topButce, p.sozlesme FROM Projects p INNER JOIN ProjectsFlow pf ON p.id = pf.projeId", "ProjectsTableMapping"); | |
List<ProjectsTables> proTableList= projectsList.getResultList(); | |
model.addAttribute("authenticatedUserName", WordUtils.capitalize(userInfo.getFirstName())); | |
model.addAttribute("authenticatedUserLastname",WordUtils.capitalize(userInfo.getLastName())); | |
model.addAttribute("authenticatedUserId",userInfo.getId()); | |
model.addAttribute("projectsListt",proTableList); | |
return "/homepage"; | |
}else{ | |
auth.setAuthenticated(false); | |
return "redirect:/j_spring_security_logout"; | |
} | |
} | |
@RequestMapping(value = "/userProfile" , method = RequestMethod.GET) | |
public String userProfile(final Model model, @RequestParam("user") final String user){ | |
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
if(auth.getName().equals(user)) { | |
final User userInfo = userRepository.findByEmail(auth.getName()); | |
//final Role userRole = roleRepository.findByName(auth.getName()); | |
model.addAttribute("authenticatedUserName", WordUtils.capitalize(userInfo.getFirstName())); | |
model.addAttribute("authenticatedUserLastname", WordUtils.capitalize(userInfo.getLastName())); | |
model.addAttribute("authenticatedUserPass", userInfo.getPassword()); | |
model.addAttribute("authenticatedUserRoles", getRoles(userInfo.getRoles())); | |
model.addAttribute("authenticatedUserId", userInfo.getId()); | |
return "/userProfile"; | |
}else{ | |
auth.setAuthenticated(false); | |
//SecurityContextHolder.clearContext(); | |
return "redirect:/j_spring_security_logout"; | |
} | |
} | |
@RequestMapping(value = "/user/userProfile" , method = RequestMethod.GET) | |
public String changePassSucc( @RequestParam("user") final String user){ | |
final User userInfo=userRepository.findByEmail(user); | |
final Role userRole=roleRepository.findByName(user); | |
return "/userProfile"; | |
} | |
// UTIL | |
public final Collection<? extends GrantedAuthority> getAuthorities(final Collection<Role> roles) { | |
return getGrantedAuthorities(getPrivileges(roles)); | |
} | |
private final List<String> getPrivileges(final Collection<Role> roles) { | |
final List<String> privileges = new ArrayList<String>(); | |
final List<Privilege> collection = new ArrayList<Privilege>(); | |
for (Role role : roles) { | |
collection.addAll(role.getPrivileges()); | |
} | |
for (final Privilege item : collection) { | |
privileges.add(item.getName()); | |
} | |
return privileges; | |
} | |
private final String getRoles(final Collection<Role> roles) { | |
//final List<String> privileges = new ArrayList<String>(); | |
final List<String> collection = new ArrayList<String>(); | |
for (Role role : roles) { | |
collection.add(role.getName()); | |
} | |
String str = collection.toString(); | |
return str; | |
} | |
private final List<GrantedAuthority> getGrantedAuthorities(final List<String> privileges) { | |
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); | |
for (final String privilege : privileges) { | |
authorities.add(new SimpleGrantedAuthority(privilege)); | |
} | |
return authorities; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment