提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.modular.system.factory; |
|
17 |
|
|
18 |
import cn.hutool.core.bean.BeanUtil; |
|
19 |
import cn.hutool.core.date.DateUtil; |
|
20 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
21 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
22 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
23 |
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory; |
|
24 |
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus; |
|
25 |
import cn.stylefeng.guns.sys.modular.system.entity.User; |
|
26 |
import cn.stylefeng.guns.sys.modular.system.model.UserDto; |
|
27 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
28 |
import org.springframework.beans.BeanUtils; |
|
29 |
|
|
30 |
import java.util.Date; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.Map; |
|
33 |
|
|
34 |
/** |
|
35 |
* 用户创建工厂 |
|
36 |
* |
|
37 |
* @author fengshuonan |
|
38 |
* @date 2017-05-05 22:43 |
|
39 |
*/ |
|
40 |
public class UserFactory { |
|
41 |
|
|
42 |
/** |
|
43 |
* 根据请求创建实体 |
|
44 |
*/ |
|
45 |
public static User createUser(UserDto userDto, String md5Password, String salt) { |
|
46 |
if (userDto == null) { |
|
47 |
return null; |
|
48 |
} else { |
|
49 |
User user = new User(); |
|
50 |
BeanUtils.copyProperties(userDto, user); |
|
51 |
user.setCreateTime(new Date()); |
|
52 |
user.setStatus(ManagerStatus.OK.getCode()); |
|
53 |
user.setPassword(md5Password); |
|
54 |
user.setSalt(salt); |
|
55 |
return user; |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* 更新user |
|
61 |
*/ |
|
62 |
public static User editUser(UserDto newUser, User oldUser) { |
|
63 |
if (newUser == null || oldUser == null) { |
|
64 |
return oldUser; |
|
65 |
} else { |
|
66 |
if (ToolUtil.isNotEmpty(newUser.getAvatar())) { |
|
67 |
oldUser.setAvatar(newUser.getAvatar()); |
|
68 |
} |
|
69 |
if (ToolUtil.isNotEmpty(newUser.getName())) { |
|
70 |
oldUser.setName(newUser.getName()); |
|
71 |
} |
|
72 |
if (ToolUtil.isNotEmpty(newUser.getBirthday())) { |
|
73 |
oldUser.setBirthday(newUser.getBirthday()); |
|
74 |
} |
|
75 |
if (ToolUtil.isNotEmpty(newUser.getDeptId())) { |
|
76 |
oldUser.setDeptId(newUser.getDeptId()); |
|
77 |
} |
|
78 |
if (ToolUtil.isNotEmpty(newUser.getSex())) { |
|
79 |
oldUser.setSex(newUser.getSex()); |
|
80 |
} |
|
81 |
if (ToolUtil.isNotEmpty(newUser.getEmail())) { |
|
82 |
oldUser.setEmail(newUser.getEmail()); |
|
83 |
} |
|
84 |
if (ToolUtil.isNotEmpty(newUser.getPhone())) { |
|
85 |
oldUser.setPhone(newUser.getPhone()); |
|
86 |
} |
|
87 |
return oldUser; |
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 过滤不安全字段并转化为map |
|
93 |
*/ |
|
94 |
public static Map<String, Object> removeUnSafeFields(User user) { |
|
95 |
if (user == null) { |
|
96 |
return new HashMap<>(); |
|
97 |
} else { |
|
98 |
Map<String, Object> map = BeanUtil.beanToMap(user); |
|
99 |
map.remove("password"); |
|
100 |
map.remove("salt"); |
|
101 |
map.put("birthday", DateUtil.formatDate(user.getBirthday())); |
|
102 |
return map; |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* 通过用户表的信息创建一个登录用户 |
|
108 |
*/ |
|
109 |
public static LoginUser createLoginUser(User user) { |
|
110 |
LoginUser loginUser = new LoginUser(); |
|
111 |
|
|
112 |
if (user == null) { |
|
113 |
return loginUser; |
|
114 |
} |
|
115 |
|
|
116 |
loginUser.setId(user.getUserId()); |
|
117 |
loginUser.setAccount(user.getAccount()); |
|
118 |
loginUser.setDeptId(user.getDeptId()); |
|
119 |
loginUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptId())); |
|
120 |
loginUser.setName(user.getName()); |
|
121 |
loginUser.setEmail(user.getEmail()); |
|
122 |
|
|
123 |
loginUser.setAvatar("/api/system/preview/" + user.getAvatar()); |
|
124 |
|
|
125 |
return loginUser; |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* 判断用户是否是从oauth2登录过来的 |
|
130 |
*/ |
|
131 |
public static boolean oauth2Flag() { |
|
132 |
String account = LoginContextHolder.getContext().getUser().getAccount(); |
|
133 |
if (account.startsWith(ConstantsContext.getOAuth2UserPrefix())) { |
|
134 |
return true; |
|
135 |
} else { |
|
136 |
return false; |
|
137 |
} |
|
138 |
} |
|
139 |
} |