Commit 8d8e2d91 authored by issei.miyajima's avatar issei.miyajima

user権限変更非同期

parent a71348ea
...@@ -14,7 +14,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter{ ...@@ -14,7 +14,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override @Override
public void configure(WebSecurity web) throws Exception{ public void configure(WebSecurity web) throws Exception{
web.debug(false).ignoring().antMatchers("/images/**", "/js/**", "/css/**", "/fonts/**", "/limited/inputCart", "/limited/buy", "/limited/editCart"); web.debug(false).ignoring().antMatchers("/images/**", "/js/**", "/css/**", "/fonts/**", "/limited/inputCart", "/limited/buy", "/limited/editCart", "/limited/admin/management/1/changeRole");
} }
@Override @Override
...@@ -35,6 +35,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter{ ...@@ -35,6 +35,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter{
.invalidateHttpSession(true) .invalidateHttpSession(true)
.deleteCookies("JSESSIONID") //ログアウト時キャッシュを削除する処理 .deleteCookies("JSESSIONID") //ログアウト時キャッシュを削除する処理
.logoutSuccessUrl("/limited/login"); //ログアウト後に表示するURL .logoutSuccessUrl("/limited/login"); //ログアウト後に表示するURL
//http.csrf().disable();
} }
@Bean //パスワードのハッシュか @Bean //パスワードのハッシュか
......
...@@ -204,19 +204,19 @@ public class ManagementController { ...@@ -204,19 +204,19 @@ public class ManagementController {
return "redirect:/limited/admin/management"; return "redirect:/limited/admin/management";
} }
//user権限変更 // //user権限変更
@PostMapping(path = "{id}") // @PostMapping(path = "{id}")
String changeRoles(Integer id, @AuthenticationPrincipal LoginUser loginUser){ // String changeRoles(Integer id, @AuthenticationPrincipal LoginUser loginUser){
User user = loginUserDetailsService.findOne(id); // User user = loginUserDetailsService.findOne(id);
String role = user.getRoles(); // String role = user.getRoles();
if(role.equals("ADMIN")){ // if(role.equals("ADMIN")){
user.setRoles("USER"); // user.setRoles("USER");
}else{ // }else{
user.setRoles("ADMIN"); // user.setRoles("ADMIN");
} // }
loginUserDetailsService.create(user); // loginUserDetailsService.create(user);
return "redirect:/limited/admin/management"; // return "redirect:/limited/admin/management";
} // }
} }
package com.example.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.User;
import com.example.service.LoginUser;
import com.example.service.LoginUserDetailsService;
@RestController
@RequestMapping("limited/admin/management")
public class ManagementRestController {
@Autowired
LoginUserDetailsService loginUserDetailsService;
//user権限変更
@PostMapping(path = "{id}/changeRole")
User changeRoles(@PathVariable Integer id, @AuthenticationPrincipal LoginUser loginUser, @RequestBody User userdata){
User user = loginUserDetailsService.findOne(id);
//String role = userdata.getRoles();
if(userdata.getRoles().equals("ADMIN")){
user.setRoles("USER");
}else{
user.setRoles("ADMIN");
}
loginUserDetailsService.create(user);
return user;
}
}
...@@ -7,15 +7,44 @@ $('#user_button').on('click', function(){ ...@@ -7,15 +7,44 @@ $('#user_button').on('click', function(){
$('#shoe_management').hide(); $('#shoe_management').hide();
}) })
//権限変更の確認アラート
$('.roleBtn').on('click', function(){ //権限変更の確認アラート
if(!confirm('本当に権限を変更しますか?')) { $('.roleBtn').click(function(event){
return false; var token = $("meta[name='_csrf']").attr("content");
}else{ var header = $("meta[name='_csrf_header']").attr("content");
//user権限変更 $(document).ajaxSend(function(e, xhr, options) {
} xhr.setRequestHeader(header, token);
});
console.log("ok");
if(confirm('本当に権限を変更しますか?')) {
//user権限変更
let userIntId = event.target.parentElement.parentElement.parentElement.parentElement.parentElement.children[0].textContent;
let userId = parseInt(event.target.parentElement.parentElement.parentElement.parentElement.parentElement.children[0].textContent);
let userRole = event.target.parentElement.parentElement.parentElement.parentElement.parentElement.children[2].textContent;
let RoleForm = event.target.parentElement.parentElement.parentElement.parentElement.parentElement.children[2];
let data = {
id: userId,
roles: userRole
};
$.ajax({
type:'POST',
url:'management/'+ userIntId + '/changeRole',
contentType: 'application/json',
dataType: "json",
data: JSON.stringify(data)
})
.done(function(data1,textStatus,jqXHR){
//レスポンスからuserRole取得
let responseRole = data1.roles;
//HTML上で書き換え
RoleForm.textContent = responseRole;
})
}
}) })
//商品管理 //商品管理
$('#shoe_button').on('click', function(){ $('#shoe_button').on('click', function(){
$('#user_management').hide(); $('#user_management').hide();
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
<html class="no-js" lang="en" xmlns:th="http://www.thymeleaf.org" <html class="no-js" lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head> <head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>Limited</title> <title>Limited</title>
...@@ -42,17 +44,20 @@ ...@@ -42,17 +44,20 @@
<tr th:each="users : ${users}"> <tr th:each="users : ${users}">
<td th:text="${users.id}">1</td> <td th:text="${users.id}">1</td>
<td th:text="${users.name}">demo</td> <td th:text="${users.name}">demo</td>
<td name="roles" th:text="${users.roles}"></td> <td name="roles" th:text="${users.roles}" class="userRoleForm"></td>
<td> <td>
<div th:object="${user}" th:switch="${user.id}"> <div th:object="${user}" th:switch="${user.id}">
<div th:case="${users.id}"> <div th:case="${users.id}">
<a>ログイン中のため変更できません</a> <a>ログイン中のため変更できません</a>
</div> </div>
<div th:case="*"> <div th:case="*">
<form th:action="@{/limited/admin/management/{id}(id=${users.id})}" th:method="post"> <form>
<button type="button" name="form" value="権限変更" class="roleBtn">権限変更</button>
</form>
<!-- <form th:action="@{/limited/admin/management/{id}(id=${users.id})}" th:method="post">
<input type="submit" name="form" value="権限変更" class="roleBtn"/> <input type="submit" name="form" value="権限変更" class="roleBtn"/>
<input type="hidden" name="id" th:value="${users.id}"/> <input type="hidden" name="id" th:value="${users.id}"/>
</form> </form> -->
</div> </div>
</div> </div>
</td> </td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment