dzilajaca relacja users roles
This commit is contained in:
1 parent
2be2cdf064
commit
abbac0b416
8 files changed
+134
-7
No files matched your search
+2
-2
@@ -36,8 +36,8 @@ dependencies {
|
||||
compile 'org.json:json:20080701'
|
||||
|
||||
compile("org.springframework.boot:spring-boot-starter-web")
|
||||
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
|
||||
//providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
|
||||
testCompile("org.springframework.boot:spring-boot-starter-test")
|
||||
providedRuntime("javax.servlet:jstl")
|
||||
providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
|
||||
//providedRuntime("org.apache.tomcat.embed:tomcat-embed-jasper")
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
package application;
|
||||
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import application.utils.TokensGenerator;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -14,6 +18,9 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 01.04.14.
|
||||
*/
|
||||
@@ -24,6 +31,7 @@ import org.springframework.web.servlet.view.JstlView;
|
||||
@EnableScheduling
|
||||
public class Config extends SpringBootServletInitializer {
|
||||
|
||||
|
||||
@Scheduled(fixedRate = 50000)
|
||||
public void generatePassword() {
|
||||
TokensGenerator.generatePassword();
|
||||
@@ -32,6 +40,10 @@ public class Config extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
|
||||
ConfigurableApplicationContext context = SpringApplication.run(Config.class, args);
|
||||
UserRepository userRepository=context.getBean(UserRepository.class);
|
||||
RoleRepository roleRepository=context.getBean(RoleRepository.class);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -32,6 +32,7 @@ public class RoleController {
|
||||
|
||||
|
||||
@RequestMapping("/{roleId}")
|
||||
@ResponseBody
|
||||
public String getRole(@PathVariable("roleId") Long roleId) {
|
||||
Role role = roleRepository.findOne(roleId);
|
||||
return "role: "+role.toString();
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package application.controllers;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class SampleController {
|
||||
|
||||
@RequestMapping("/hello")
|
||||
public String index(Model model) {
|
||||
model.addAttribute("message", "HELLO!");
|
||||
return "hello";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package application.controllers;
|
||||
|
||||
import application.model.Role;
|
||||
import application.model.User;
|
||||
import application.repositories.RoleRepository;
|
||||
import application.repositories.UserRepository;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@@ -14,6 +15,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -26,6 +30,11 @@ public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private RoleRepository roleRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@RequestMapping(value = "/add")
|
||||
@ResponseBody
|
||||
@@ -47,10 +56,67 @@ public class UserController {
|
||||
return "user: "+user.toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/init")
|
||||
public
|
||||
@ResponseBody
|
||||
String initialise() {
|
||||
|
||||
Role ceo = new Role("CEO","101010");
|
||||
Role worker = new Role("Worker","12345");
|
||||
roleRepository.save(new Role("Cleaner","54321"));
|
||||
ceo = roleRepository.save(ceo);
|
||||
|
||||
|
||||
User user1 = new User("Łukasz", "Goliński", "boss@nfc.pl");
|
||||
user1.addRole(worker);
|
||||
user1.addRole(ceo);
|
||||
userRepository.save(user1);
|
||||
|
||||
|
||||
ArrayList<Role> roles = (ArrayList<Role>) roleRepository.findAll();
|
||||
|
||||
for(Role r: roles){
|
||||
System.out.println(r.getRoleName());
|
||||
}
|
||||
|
||||
Role r = roles.get(0);
|
||||
|
||||
User user3 = new User("Monika", "Bigas", "boss_monika@nfc.pl");
|
||||
user3.addRole(worker);
|
||||
user3.addRole(ceo);
|
||||
userRepository.save(user3);
|
||||
//entityManager.merge(user3);
|
||||
|
||||
User user2 = new User("Marcin", "Michałek", "boss_marcin@nfc.pl");
|
||||
user2.addRole(ceo);
|
||||
userRepository.save(user2);
|
||||
|
||||
|
||||
|
||||
|
||||
ArrayList<User> users = (ArrayList<User>) userRepository.findAll();
|
||||
for(User u: users){
|
||||
System.out.println(u.getFirstName()+" "+u.getLastName());
|
||||
}
|
||||
|
||||
for (User user : userRepository.findAll()) {
|
||||
System.out.println("USER: " + user.getFirstName() + " " + user.getLastName() + " " + user.getEmail());
|
||||
System.out.println("roles: ");
|
||||
for (Role role : user.getRoles()) {
|
||||
System.out.println(" -" + role.getRoleName());
|
||||
}
|
||||
System.out.println("enduser");
|
||||
System.out.println("");
|
||||
}
|
||||
return "initialised";
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/all")
|
||||
public
|
||||
@ResponseBody
|
||||
String listUsersJson(ModelMap model) throws JSONException {
|
||||
|
||||
JSONArray userArray = new JSONArray();
|
||||
for (User user : userRepository.findAll()) {
|
||||
JSONObject userJSON = new JSONObject();
|
||||
@@ -58,7 +124,8 @@ public class UserController {
|
||||
userJSON.put("firstname", user.getFirstName());
|
||||
userArray.put(userJSON);
|
||||
}
|
||||
return userArray.toString();
|
||||
return userArray.toString();
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/remove/{userId}")
|
||||
|
||||
@@ -50,4 +50,21 @@ public class Role {
|
||||
public Long getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Role role = (Role) o;
|
||||
|
||||
if (!password.equals(role.password)) return false;
|
||||
if (!roleId.equals(role.roleId)) return false;
|
||||
if (!roleName.equals(role.roleName)) return false;
|
||||
if (users != null ? !users.equals(role.users) : role.users != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -20,15 +20,23 @@ public class User {
|
||||
@Basic
|
||||
private String email;
|
||||
|
||||
public User(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
|
||||
@JoinTable(
|
||||
name="User_Role",
|
||||
joinColumns = @JoinColumn( name="User_ID"),
|
||||
inverseJoinColumns = @JoinColumn( name="Role_ID")
|
||||
)
|
||||
private Set<Role> roles = new HashSet<Role>(
|
||||
0);
|
||||
inverseJoinColumns = @JoinColumn( name="Role_ID"))
|
||||
private Set<Role> roles = new HashSet<Role>(0);
|
||||
|
||||
//getter and setter
|
||||
private Set<Role> getRole(){
|
||||
return roles;
|
||||
};
|
||||
@@ -72,4 +80,9 @@ public class User {
|
||||
public void setRoles(Set<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
//end
|
||||
|
||||
public void addRole(Role role){
|
||||
roles.add(role);
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,5 @@ import org.springframework.data.repository.CrudRepository;
|
||||
* Created by lukasgol on 21.04.14.
|
||||
*/
|
||||
public interface RoleRepository extends CrudRepository<Role, Long> {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user