Commit be033bdb authored by zjy's avatar zjy

user/role

parent 661946b8
......@@ -60,76 +60,24 @@ public class TrightController {
*/
@RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object listTrights(){
List<TrightVo> trightVos = trightDao.listAll();
List<TrightVo> list = Lists.newArrayList();
setPermissionsList(0L,trightVos, list);
Result result = new Result(0,"SUCCESS",list);
return result;
}
/**
* 菜单列表
*
* @param pId
* @param trightVos
* @param list
*/
private void setPermissionsList(Long pId, List<TrightVo> trightVos, List<TrightVo> list) {
for (TrightVo per : trightVos) {
if (per.getModuleId().equals(pId)) {
list.add(per);
if (trightVos.stream().filter(p -> p.getModuleId().equals(per.getTrId())).findAny() != null) {
setPermissionsList(per.getTrId(), trightVos, list);
}
}
List<TrightVo> list = trightServiceImpl.listAllTright();
if (list == null){
return new Result(4001,"暂无权限,请先添加",null);
}
return new Result(0,"SUCCESS",list);
}
/**
* 查询一级权限
* @return
*/
@RequestMapping(value = "/parent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "/firstLevel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object parentTrights(){
List<TrightVo> parentTrights = trightDao.listParents();
Result result = new Result(0,"SUCCESS",parentTrights);
return result;
}
/**
* 查询所有权限
* @return
*/
@RequestMapping(value = "/all", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object allTrights(){
List<TrightVo> trightVos = trightDao.listAll();
JSONArray array = new JSONArray();
setPermissionsTree(0L,trightVos, array);
Result result = new Result(0,"SUCCESS",array);
return result;
}
/**
* 菜单树
*
* @param pId
* @param trightVos
* @param array
*/
private void setPermissionsTree(Long pId, List<TrightVo> trightVos, JSONArray array) {
for (TrightVo per : trightVos) {
if (per.getModuleId().equals(pId)) {
String string = JSONObject.toJSONString(per);
JSONObject parent = (JSONObject) JSONObject.parse(string);
array.add(parent);
if (trightVos.stream().filter(p -> p.getModuleId().equals(per.getTrId())).findAny() != null) {
JSONArray child = new JSONArray();
parent.put("child", child);
setPermissionsTree(per.getTrId(), trightVos, child);
}
}
List<TrightVo> list = trightServiceImpl.listLevel1();
if (list == null){
return new Result(4001,"暂无一级权限,请先添加",null);
}
return new Result(0,"SUCCESS",list);
}
/**
......@@ -137,11 +85,13 @@ public class TrightController {
* @param id
* @return
*/
@RequestMapping(value = "/role", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object tright(@PathVariable Long id){
trightDao.getById(id);
Result result = new Result(0,"SUCCESS",null);
return null;
@RequestMapping(value = "/role/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object tright(@PathVariable String id){
TrightVo trightVo = trightServiceImpl.getTrightById(id);
if (trightVo == null){
return new Result(4001,"未查询到该权限",null);
}
return new Result(0,"SUCCESS",trightVo);
}
/**
......@@ -149,9 +99,9 @@ public class TrightController {
* @param id
* @return
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object delete(@PathVariable Long id){
trightServiceImpl.delete(id);
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object delete(@PathVariable String id){
trightServiceImpl.deleteTrightById(id);
Result result = new Result(0,"SUCCESS",null);
return result;
}
......
......@@ -39,6 +39,8 @@ public class UserController {
//throw new IllegalArgumentException(userDto.getUserName() + "已存在");
}
//不存在,保存用户信息
String saltPassword = userServiceImpl.passwordEncoder(userDto.getPassword());
userDto.setPassword(saltPassword);
userDao.create(userDto);
if (userDto.getRoleIds()!=null&&userDto.getRoleIds().size()!=0){
userServiceImpl.userAndRole(userDto);
......@@ -57,6 +59,7 @@ public class UserController {
@RequestMapping(value = "/update", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object updateUser(@RequestBody UserDto userDto){
UserVo u = userDto;
u.setPassword(userServiceImpl.passwordEncoder(u.getPassword()));
userDao.update(u);
userServiceImpl.userAndRole(userDto);
Result result = new Result(0,"SUCCESS",null);
......
......@@ -9,15 +9,15 @@ public interface TrightDao extends BaseDao {
int checkValid(TrightVo trightVo);
int deleteRoleTright(Long id);
TrightVo getTrightById(String id);
int delete(Long id);
int deleteRoleTright(String id);
int deleteByParentId(Long id);
//int deleteTrightById(String id);
TrightVo getById(Long id);
int deleteByParentId(String id);
List<TrightVo> listAll();
List<TrightVo> listParents();
List<TrightVo> listLevel1();
}
......@@ -10,6 +10,9 @@ import java.util.List;
public class TrightDaoImpl extends BaseDaoImpl implements TrightDao {
private static final String CHECK_VALID = "CHECK_VALID";
private static final String GET_TRIGHT_BY_ID = "GET_TRIGHT_BY_ID";
private static final String LIST_ALL = "LIST_ALL";
private static final String LIST_LEVEL_ONE = "LIST_LEVEL_ONE";
@Override
protected void init() {
......@@ -19,6 +22,13 @@ public class TrightDaoImpl extends BaseDaoImpl implements TrightDao {
.add("name","and a.name = ?")
.add("url","and a.url = ?")
.add("id","and a.id <> ?");
Table.add(GET_TRIGHT_BY_ID,"select * from pd_authority where 1=1")
.add("id","and id = ?");
Table.add(LIST_ALL,"select * from pd_authority where 1=1");
Table.add(LIST_LEVEL_ONE,"select * from pd_authority where pid=null");
}
@Override
......@@ -27,33 +37,30 @@ public class TrightDaoImpl extends BaseDaoImpl implements TrightDao {
}
@Override
public int deleteRoleTright(Long id) {
return 0;
public TrightVo getTrightById(String id) {
return this.queryFirst(TrightVo.class,GET_TRIGHT_BY_ID,id);
}
@Override
public int delete(Long id) {
public int deleteRoleTright(String id) {
return 0;
}
@Override
public int deleteByParentId(Long id) {
public int deleteByParentId(String id) {
return 0;
}
@Override
public TrightVo getById(Long id) {
return null;
}
@Override
public List<TrightVo> listAll() {
return null;
return this.query(TrightVo.class,LIST_ALL,null);
}
@Override
public List<TrightVo> listParents() {
return null;
public List<TrightVo> listLevel1() {
return this.query(TrightVo.class,LIST_LEVEL_ONE,null);
}
......
......@@ -2,12 +2,20 @@ package com.pangding.web.tright.service;
import com.pangding.web.tright.vo.TrightVo;
import java.util.List;
public interface TrightService {
void save(TrightVo trightVo);
void update(TrightVo trightVo);
void delete(Long id);
TrightVo getTrightById(String id);
void deleteTrightById(String id);
Boolean checkValid(TrightVo trightVo);
List<TrightVo> listAllTright();
List<TrightVo> listLevel1();
}
......@@ -12,7 +12,7 @@ public interface UserService {
UserVo getUser(UserVo userVo);
String passwordEncoder(String credentials, String salt);
String passwordEncoder(String password);
List<UserVo> listUserVo();
......
......@@ -6,6 +6,8 @@ import com.pangding.web.tright.vo.TrightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TrightServiceImpl implements TrightService {
......@@ -29,9 +31,28 @@ public class TrightServiceImpl implements TrightService {
}
@Override
public void delete(Long id) {
public TrightVo getTrightById(String id) {
TrightVo trightVo = trightDao.getTrightById(id);
return trightVo != null ? trightVo : null;
}
@Override
public void deleteTrightById(String id) {
trightDao.deleteRoleTright(id);
trightDao.delete(id);
//trightDao.deleteTrightById(id);
trightDao.remove(id);
trightDao.deleteByParentId(id);
}
@Override
public List<TrightVo> listAllTright() {
List<TrightVo> list = trightDao.listAll();
return (list != null && list.size() != 0) ? list : null;
}
@Override
public List<TrightVo> listLevel1() {
List<TrightVo> list = trightDao.listLevel1();
return (list != null && list.size() != 0) ? list : null;
}
}
......@@ -3,6 +3,7 @@ package com.pangding.web.tright.service.impl;
import com.pangding.web.tright.dao.UserDao;
import com.pangding.web.tright.dto.UserDto;
import com.pangding.web.tright.service.UserService;
import com.pangding.web.tright.utils.Md5AndSalt;
import com.pangding.web.tright.vo.UserRoleRelation;
import com.pangding.web.tright.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -47,9 +48,7 @@ public class UserServiceImpl implements UserService {
}
@Override
public String passwordEncoder(String credentials, String salt) {
return null;
}
public String passwordEncoder(String password) { return Md5AndSalt.generate(password); }
@Override
public List<String> listRoleIdList(UserVo userVo) {
......
package com.pangding.web.tright.utils;
import org.apache.commons.codec.binary.Hex;
import java.security.MessageDigest;
import java.util.Random;
public class Md5AndSalt {
public static String generate(String password) {
Random r = new Random();
StringBuilder sb = new StringBuilder(16);
sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
int len = sb.length();
if (len < 16) {
for (int i = 0; i < 16 - len; i++) {
sb.append("0");
}
}
String salt = sb.toString();
password = md5Hex(password + salt);
char[] cs = new char[48];
for (int i = 0; i < 48; i += 3) {
cs[i] = password.charAt(i / 3 * 2);
char c = salt.charAt(i / 3);
cs[i + 1] = c;
cs[i + 2] = password.charAt(i / 3 * 2 + 1);
}
return new String(cs);
}
/**
* 获取十六进制字符串形式的MD5摘要
*/
private static String md5Hex(String src) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bs = md5.digest(src.getBytes());
return new String(new Hex().encode(bs));
} catch (Exception e) {
return null;
}
}
public static boolean verify(String password, String md5) {
char[] cs1 = new char[32];
char[] cs2 = new char[16];
for (int i = 0; i < 48; i += 3) {
cs1[i / 3 * 2] = md5.charAt(i);
cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
cs2[i / 3] = md5.charAt(i + 1);
}
String salt = new String(cs2);
return md5Hex(password + salt).equals(new String(cs1));
}
}
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