100 lines
3.2 KiB
Java
100 lines
3.2 KiB
Java
package application.controllers;
|
|
|
|
import application.model.Role;
|
|
import application.repositories.RoleRepository;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.ModelMap;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
/**
|
|
* Created by lukasgol on 21.04.14.
|
|
*/
|
|
@Controller
|
|
@RequestMapping(value = "/roles")
|
|
public class RoleController {
|
|
|
|
public void setRoleRepository(RoleRepository roleRepository) {
|
|
this.roleRepository = roleRepository;
|
|
}
|
|
|
|
@Autowired
|
|
private RoleRepository roleRepository;
|
|
|
|
@RequestMapping(value = "/add")
|
|
public Role addRole(@RequestParam String rolename,@RequestParam String password) {
|
|
Role role = new Role(rolename,password);
|
|
Role result = roleRepository.save(role);
|
|
return result;
|
|
}
|
|
|
|
@RequestMapping(value = "/addSampleRoles")
|
|
@ResponseBody
|
|
public String addSampleRoles() {
|
|
roleRepository.save(new Role("CLEANER", "shityJob"));
|
|
roleRepository.save(new Role("BOSS", "fuckYea"));
|
|
roleRepository.save(new Role("SECURITY", "ochroniarzPatryk"));
|
|
|
|
return "roles added";
|
|
}
|
|
|
|
@RequestMapping("/{roleId}")
|
|
@ResponseBody
|
|
public String getRole(@PathVariable("roleId") Long roleId) throws JSONException{
|
|
Role role = roleRepository.findOne(roleId);
|
|
JSONArray roleArray = new JSONArray();
|
|
JSONObject roleJSON = new JSONObject();
|
|
roleJSON.put("id", role.getRoleId());
|
|
roleJSON.put("name", role.getRoleName());
|
|
roleJSON.put("password", role.getPassword());
|
|
roleArray.put(roleJSON);
|
|
|
|
return roleArray.toString();
|
|
}
|
|
|
|
@RequestMapping
|
|
public
|
|
@ResponseBody
|
|
String listRolesJson(ModelMap model) throws JSONException {
|
|
JSONArray roleArray = new JSONArray();
|
|
for (Role role : roleRepository.findAll()) {
|
|
JSONObject roleJSON = new JSONObject();
|
|
roleJSON.put("role", role.getRoleName());
|
|
roleJSON.put("password", role.getPassword());
|
|
roleArray.put(roleJSON);
|
|
}
|
|
return roleArray.toString();
|
|
}
|
|
|
|
@RequestMapping("/getRoles")
|
|
public
|
|
@ResponseBody
|
|
String getRoles() throws JSONException {
|
|
JSONObject accessCodes = new JSONObject();
|
|
accessCodes.put("success",true);
|
|
|
|
JSONArray roleArray = new JSONArray();
|
|
for (Role role : roleRepository.findAll()) {
|
|
JSONObject roleJSON = new JSONObject();
|
|
roleJSON.put("role", role.getRoleName());
|
|
roleJSON.put("password", role.getPassword());
|
|
roleArray.put(roleJSON);
|
|
}
|
|
accessCodes.put("accessCodes", roleArray);
|
|
|
|
return accessCodes.toString();
|
|
}
|
|
|
|
@RequestMapping("/remove/{roleId}")
|
|
public String removeRole(@PathVariable("roleId") Long roleId) {
|
|
roleRepository.delete(roleId);
|
|
return "role deleted: "+roleId;
|
|
}
|
|
}
|