<commit_before>/**
 * 版权所有 (TMS)
 */
package com.lhjz.portal.controller;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lhjz.portal.base.BaseController;
import com.lhjz.portal.component.MailSender2;
import com.lhjz.portal.entity.security.Authority;
import com.lhjz.portal.entity.security.AuthorityId;
import com.lhjz.portal.entity.security.User;
import com.lhjz.portal.model.Mail;
import com.lhjz.portal.model.RespBody;
import com.lhjz.portal.pojo.Enum.Action;
import com.lhjz.portal.pojo.Enum.Role;
import com.lhjz.portal.pojo.Enum.Status;
import com.lhjz.portal.pojo.Enum.Target;
import com.lhjz.portal.pojo.UserForm;
import com.lhjz.portal.repository.AuthorityRepository;
import com.lhjz.portal.repository.UserRepository;
import com.lhjz.portal.util.DateUtil;
import com.lhjz.portal.util.MapUtil;
import com.lhjz.portal.util.StringUtil;
import com.lhjz.portal.util.TemplateUtil;
import com.lhjz.portal.util.ThreadUtil;
import com.lhjz.portal.util.WebUtil;

/**
 * 
 * @author xi
 * 
 * @date 2015年3月28日 下午1:19:05
 * 
 */
@Controller
@RequestMapping("admin/user")
public class UserController extends BaseController {

	static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	UserRepository userRepository;

	@Autowired
	AuthorityRepository authorityRepository;

	@Autowired
	PasswordEncoder passwordEncoder;

	@Autowired
	MailSender2 mailSender;

	String loginAction = "admin/login";

	private RespBody createUser(String role, String baseURL,
			UserForm userForm) {

		if (userRepository.exists(StringUtils.trim(userForm.getUsername()))) {
			logger.error("添加用户已经存在, ID: {}",
					StringUtils.trim(userForm.getUsername()));
			return RespBody.failed("添加用户已经存在!");
		}

		// save username and password
		final User user = new User();
		user.setUsername(StringUtils.trim(userForm.getUsername()));
		user.setPassword(passwordEncoder
				.encode(StringUtils.trim(userForm.getPassword())));
		user.setEnabled(userForm.getEnabled());
		user.setCreateDate(new Date());
		user.setName(StringUtils.trim(userForm.getName()));
		user.setMails(StringUtils.trim(userForm.getMail()));

		userRepository.saveAndFlush(user);

		log(Action.Create, Target.User, user);

		// save default authority `ROLE_USER`
		Authority authority = new Authority();
		authority
				.setId(new AuthorityId(StringUtils.trim(userForm.getUsername()),
						Role.ROLE_USER.name()));

		authorityRepository.saveAndFlush(authority);

		log(Action.Create, Target.Authority, authority);

		if (role.equalsIgnoreCase("admin")) {
			Authority authority2 = new Authority();
			authority2.setId(
					new AuthorityId(StringUtils.trim(userForm.getUsername()),
							Role.ROLE_ADMIN.name()));

			authorityRepository.saveAndFlush(authority2);

			log(Action.Create, Target.Authority, authority2);
		}

		final String userRole = role;

		final String href = baseURL;

		final Mail mail = Mail.instance().addUsers(user);

		final UserForm userForm2 = userForm;

		final String loginUrl = baseURL + loginAction + "?username="
				+ userForm.getUsername() + "&password="
				+ userForm.getPassword();

		if (mail.get().length > 0) {
			ThreadUtil.exec(() -> {

				try {
					Thread.sleep(3000);
					mailSender.sendHtml(
							String.format("TMS-用户创建_%s",
									DateUtil.format(new Date(),
											DateUtil.FORMAT7)),
							TemplateUtil.process("templates/mail/user-create",
									MapUtil.objArr2Map("user", userForm2,
											"userRole", userRole, "href", href,
											"loginUrl", loginUrl)),
							mail.get());
					logger.info("创建用户邮件发送成功！");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("创建用户翻译邮件发送失败！");
				}

			});
		}

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "batchCreate", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody batchCreate(@RequestParam("baseURL") String baseURL,
			@RequestParam("data") String data) {

		// test,88888,user,测试用户,<EMAIL>,true
		String[] lines = data.split("\n");
		int cnt = 0;
		for (String line : lines) {
			String[] infos = line.trim().split(",");
			if (infos.length >= 5) {
				UserForm userForm = new UserForm();
				userForm.setEnabled(true);
				userForm.setMail(StringUtils.trim(infos[4]));
				userForm.setName(StringUtils.trim(infos[3]));
				userForm.setPassword(StringUtils.trim(infos[1]));
				userForm.setUsername(StringUtils.trim(infos[0]));
				userForm.setEnabled(
						"true".equalsIgnoreCase(StringUtils.trim(infos[5]))
								? true : false);

				RespBody respBody = createUser(StringUtils.trim(infos[2]),
						baseURL, userForm);
				if (respBody.isSuccess()) {
					cnt = cnt + 1;
				}
			}
		}

		if (cnt == 0) {
			return RespBody.failed();
		}

		return RespBody.succeed();
	}

	@RequestMapping(value = "batchMail", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody batchMail(@RequestParam("baseURL") String baseURL,
			@RequestParam("users") String users,
			@RequestParam("title") String title,
			@RequestParam("content") String content) {

		if (StringUtil.isEmpty(users)) {
			return RespBody.failed("发送用户不能为空!");
		}

		if (StringUtil.isEmpty(title)) {
			return RespBody.failed("发送标题不能为空!");
		}

		if (StringUtil.isEmpty(content)) {
			return RespBody.failed("发送内容不能为空!");
		}

		String[] usernames = users.split(",");
		List<User> users2 = userRepository.findAll();
		final Set<String> mails = new HashSet<>();
		for (String username : usernames) {
			for (User user : users2) {
				if (user.getUsername().equals(username)) {
					mails.add(user.getMails());
					break;
				}
			}
		}

		final User loginUser = getLoginUser();
		final String href = baseURL;
		final String title1 = title;
		final String content1 = content;

		if (mails.size() > 0) {
			ThreadUtil.exec(() -> {

				try {
					Thread.sleep(3000);
					mailSender.sendHtml(
							String.format("TMS-系统消息_%s",
									DateUtil.format(new Date(),
											DateUtil.FORMAT7)),
							TemplateUtil.process("templates/mail/mail-msg",
									MapUtil.objArr2Map("user", loginUser,
											"date", new Date(), "href", href,
											"href", href, "title", title1,
											"content", content1)),
							mails.toArray(new String[0]));
					logger.info("邮件通知发送成功！");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("邮件通知发送失败！");
				}

			});
		}

		return RespBody.succeed();
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody save(@RequestParam("role") String role,
			@RequestParam("baseURL") String baseURL, @Valid UserForm userForm,
			BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		return createUser(role, baseURL, userForm);
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody update(
			@RequestParam(value = "role", required = false) String role,
			@Valid UserForm userForm, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		User user = userRepository.findOne(userForm.getUsername());

		if (user == null) {
			logger.error("更新用户不存在! ID: {}", userForm.getUsername());
			return RespBody.failed("更新用户不存在!");
		}

		if (StringUtil.isNotEmpty(userForm.getPassword())) {

			if (userForm.getPassword().length() < 6) {
				logger.error("修改密码长度小于六位, ID: {}", userForm.getUsername());
				return RespBody.failed("修改密码长度不能小于六位!");
			}

			user.setPassword(passwordEncoder.encode(userForm.getPassword()));
		}

		if (userForm.getEnabled() != null
				&& user.getStatus() != Status.Bultin) {
			user.setEnabled(userForm.getEnabled());
		}

		if (userForm.getMail() != null) {
			user.setMails(userForm.getMail());
		}

		if (userForm.getName() != null) {
			user.setName(userForm.getName());
		}

		if (StringUtil.isNotEmpty(role)) {

			// 删除当前的权限
			Set<Authority> authorities = user.getAuthorities();
			authorities.stream().forEach((auth) -> {
				auth.setUser(null);
			});
			authorityRepository.delete(authorities);
			authorityRepository.flush();

			user.getAuthorities().clear();

			// 附加新的权限
			// add role_user
			Authority authority = new Authority();
			authority.setId(
					new AuthorityId(StringUtils.trim(userForm.getUsername()),
							Role.ROLE_USER.name()));
			authority.setUser(user);

			Authority saveAndFlush = authorityRepository
					.saveAndFlush(authority);

			user.getAuthorities().add(saveAndFlush);

			// add role_user
			if ("admin".equalsIgnoreCase(role)) {
				Authority authority2 = new Authority();
				authority2.setId(new AuthorityId(
						StringUtils.trim(userForm.getUsername()),
						Role.ROLE_ADMIN.name()));
				authority2.setUser(user);

				Authority saveAndFlush2 = authorityRepository
						.saveAndFlush(authority2);

				user.getAuthorities().add(saveAndFlush2);
			}

		}

		userRepository.saveAndFlush(user);

		log(Action.Update, Target.User, user);

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "update2", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_USER" })
	public RespBody update2(@Valid UserForm userForm,
			BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		if (!WebUtil.getUsername().equals(userForm.getUsername())) {
			logger.error("普通用户无权限修改其他用户信息!");
			return RespBody.failed("普通用户无权限修改其他用户信息!");
		}

		User user = userRepository.findOne(userForm.getUsername());

		if (user == null) {
			logger.error("更新用户不存在! ID: {}", userForm.getUsername());
			return RespBody.failed("更新用户不存在!");
		}

		if (StringUtil.isNotEmpty(userForm.getPassword())) {

			if (userForm.getPassword().length() < 6) {
				logger.error("修改密码长度小于六位, ID: {}", userForm.getUsername());
				return RespBody.failed("修改密码长度不能小于六位!");
			}

			user.setPassword(passwordEncoder.encode(userForm.getPassword()));
		}

		if (userForm.getEnabled() != null
				&& user.getStatus() != Status.Bultin) {
			user.setEnabled(userForm.getEnabled());
		}

		if (userForm.getMail() != null) {
			user.setMails(userForm.getMail());
		}

		if (userForm.getName() != null) {
			user.setName(userForm.getName());
		}

		userRepository.saveAndFlush(user);

		log(Action.Update, Target.User, user);

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody delete(@RequestParam("username") String username) {

		User user = userRepository.findOne(username);

		if (user == null) {
			logger.error("删除用户不存在! ID: {}", username);
			return RespBody.failed("删除用户不存在!");
		}

		if (user.getStatus() == Status.Bultin) {
			logger.error("内置用户,不能删除! ID: {}", username);
			return RespBody.failed("内置用户,不能删除!");
		}

		userRepository.delete(user);

		log(Action.Delete, Target.User, user);

		return RespBody.succeed(username);
	}

	@RequestMapping(value = "get", method = RequestMethod.GET)
	@ResponseBody
	@Secured({ "ROLE_ADMIN", "ROLE_USER" })
	public RespBody get(@RequestParam("username") String username) {

		User user = userRepository.findOne(username);

		if (user == null) {
			logger.error("查询用户不存在! ID: {}", username);
			return RespBody.failed("查询用户不存在!");
		}

		return RespBody.succeed(user);
	}
}
<commit_msg>user can not send mail<commit_after>/**
 * 版权所有 (TMS)
 */
package com.lhjz.portal.controller;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lhjz.portal.base.BaseController;
import com.lhjz.portal.component.MailSender2;
import com.lhjz.portal.entity.security.Authority;
import com.lhjz.portal.entity.security.AuthorityId;
import com.lhjz.portal.entity.security.User;
import com.lhjz.portal.model.Mail;
import com.lhjz.portal.model.RespBody;
import com.lhjz.portal.pojo.Enum.Action;
import com.lhjz.portal.pojo.Enum.Role;
import com.lhjz.portal.pojo.Enum.Status;
import com.lhjz.portal.pojo.Enum.Target;
import com.lhjz.portal.pojo.UserForm;
import com.lhjz.portal.repository.AuthorityRepository;
import com.lhjz.portal.repository.UserRepository;
import com.lhjz.portal.util.DateUtil;
import com.lhjz.portal.util.MapUtil;
import com.lhjz.portal.util.StringUtil;
import com.lhjz.portal.util.TemplateUtil;
import com.lhjz.portal.util.ThreadUtil;
import com.lhjz.portal.util.WebUtil;

/**
 * 
 * @author xi
 * 
 * @date 2015年3月28日 下午1:19:05
 * 
 */
@Controller
@RequestMapping("admin/user")
public class UserController extends BaseController {

	static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	UserRepository userRepository;

	@Autowired
	AuthorityRepository authorityRepository;

	@Autowired
	PasswordEncoder passwordEncoder;

	@Autowired
	MailSender2 mailSender;

	String loginAction = "admin/login";

	private RespBody createUser(String role, String baseURL,
			UserForm userForm) {

		if (userRepository.exists(StringUtils.trim(userForm.getUsername()))) {
			logger.error("添加用户已经存在, ID: {}",
					StringUtils.trim(userForm.getUsername()));
			return RespBody.failed("添加用户已经存在!");
		}

		// save username and password
		final User user = new User();
		user.setUsername(StringUtils.trim(userForm.getUsername()));
		user.setPassword(passwordEncoder
				.encode(StringUtils.trim(userForm.getPassword())));
		user.setEnabled(userForm.getEnabled());
		user.setCreateDate(new Date());
		user.setName(StringUtils.trim(userForm.getName()));
		user.setMails(StringUtils.trim(userForm.getMail()));

		userRepository.saveAndFlush(user);

		log(Action.Create, Target.User, user);

		// save default authority `ROLE_USER`
		Authority authority = new Authority();
		authority
				.setId(new AuthorityId(StringUtils.trim(userForm.getUsername()),
						Role.ROLE_USER.name()));

		authorityRepository.saveAndFlush(authority);

		log(Action.Create, Target.Authority, authority);

		if (role.equalsIgnoreCase("admin")) {
			Authority authority2 = new Authority();
			authority2.setId(
					new AuthorityId(StringUtils.trim(userForm.getUsername()),
							Role.ROLE_ADMIN.name()));

			authorityRepository.saveAndFlush(authority2);

			log(Action.Create, Target.Authority, authority2);
		}

		final String userRole = role;

		final String href = baseURL;

		final Mail mail = Mail.instance().addUsers(user);

		final UserForm userForm2 = userForm;

		final String loginUrl = baseURL + loginAction + "?username="
				+ userForm.getUsername() + "&password="
				+ userForm.getPassword();

		if (mail.get().length > 0) {
			ThreadUtil.exec(() -> {

				try {
					Thread.sleep(3000);
					mailSender.sendHtml(
							String.format("TMS-用户创建_%s",
									DateUtil.format(new Date(),
											DateUtil.FORMAT7)),
							TemplateUtil.process("templates/mail/user-create",
									MapUtil.objArr2Map("user", userForm2,
											"userRole", userRole, "href", href,
											"loginUrl", loginUrl)),
							mail.get());
					logger.info("创建用户邮件发送成功！");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("创建用户翻译邮件发送失败！");
				}

			});
		}

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "batchCreate", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody batchCreate(@RequestParam("baseURL") String baseURL,
			@RequestParam("data") String data) {

		// test,88888,user,测试用户,<EMAIL>,true
		String[] lines = data.split("\n");
		int cnt = 0;
		for (String line : lines) {
			String[] infos = line.trim().split(",");
			if (infos.length >= 5) {
				UserForm userForm = new UserForm();
				userForm.setEnabled(true);
				userForm.setMail(StringUtils.trim(infos[4]));
				userForm.setName(StringUtils.trim(infos[3]));
				userForm.setPassword(StringUtils.trim(infos[1]));
				userForm.setUsername(StringUtils.trim(infos[0]));
				userForm.setEnabled(
						"true".equalsIgnoreCase(StringUtils.trim(infos[5]))
								? true : false);

				RespBody respBody = createUser(StringUtils.trim(infos[2]),
						baseURL, userForm);
				if (respBody.isSuccess()) {
					cnt = cnt + 1;
				}
			}
		}

		if (cnt == 0) {
			return RespBody.failed();
		}

		return RespBody.succeed();
	}

	@RequestMapping(value = "batchMail", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_SUPER", "ROLE_ADMIN", "ROLE_USER" })
	public RespBody batchMail(@RequestParam("baseURL") String baseURL,
			@RequestParam("users") String users,
			@RequestParam("title") String title,
			@RequestParam("content") String content) {

		if (StringUtil.isEmpty(users)) {
			return RespBody.failed("发送用户不能为空!");
		}

		if (StringUtil.isEmpty(title)) {
			return RespBody.failed("发送标题不能为空!");
		}

		if (StringUtil.isEmpty(content)) {
			return RespBody.failed("发送内容不能为空!");
		}

		String[] usernames = users.split(",");
		List<User> users2 = userRepository.findAll();
		final Set<String> mails = new HashSet<>();
		for (String username : usernames) {
			for (User user : users2) {
				if (user.getUsername().equals(username)) {
					mails.add(user.getMails());
					break;
				}
			}
		}

		final User loginUser = getLoginUser();
		final String href = baseURL;
		final String title1 = title;
		final String content1 = content;

		if (mails.size() > 0) {
			ThreadUtil.exec(() -> {

				try {
					Thread.sleep(3000);
					mailSender.sendHtml(
							String.format("TMS-系统消息_%s",
									DateUtil.format(new Date(),
											DateUtil.FORMAT7)),
							TemplateUtil.process("templates/mail/mail-msg",
									MapUtil.objArr2Map("user", loginUser,
											"date", new Date(), "href", href,
											"href", href, "title", title1,
											"content", content1)),
							mails.toArray(new String[0]));
					logger.info("邮件通知发送成功！");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("邮件通知发送失败！");
				}

			});
		}

		return RespBody.succeed();
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody save(@RequestParam("role") String role,
			@RequestParam("baseURL") String baseURL, @Valid UserForm userForm,
			BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		return createUser(role, baseURL, userForm);
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody update(
			@RequestParam(value = "role", required = false) String role,
			@Valid UserForm userForm, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		User user = userRepository.findOne(userForm.getUsername());

		if (user == null) {
			logger.error("更新用户不存在! ID: {}", userForm.getUsername());
			return RespBody.failed("更新用户不存在!");
		}

		if (StringUtil.isNotEmpty(userForm.getPassword())) {

			if (userForm.getPassword().length() < 6) {
				logger.error("修改密码长度小于六位, ID: {}", userForm.getUsername());
				return RespBody.failed("修改密码长度不能小于六位!");
			}

			user.setPassword(passwordEncoder.encode(userForm.getPassword()));
		}

		if (userForm.getEnabled() != null
				&& user.getStatus() != Status.Bultin) {
			user.setEnabled(userForm.getEnabled());
		}

		if (userForm.getMail() != null) {
			user.setMails(userForm.getMail());
		}

		if (userForm.getName() != null) {
			user.setName(userForm.getName());
		}

		if (StringUtil.isNotEmpty(role)) {

			// 删除当前的权限
			Set<Authority> authorities = user.getAuthorities();
			authorities.stream().forEach((auth) -> {
				auth.setUser(null);
			});
			authorityRepository.delete(authorities);
			authorityRepository.flush();

			user.getAuthorities().clear();

			// 附加新的权限
			// add role_user
			Authority authority = new Authority();
			authority.setId(
					new AuthorityId(StringUtils.trim(userForm.getUsername()),
							Role.ROLE_USER.name()));
			authority.setUser(user);

			Authority saveAndFlush = authorityRepository
					.saveAndFlush(authority);

			user.getAuthorities().add(saveAndFlush);

			// add role_user
			if ("admin".equalsIgnoreCase(role)) {
				Authority authority2 = new Authority();
				authority2.setId(new AuthorityId(
						StringUtils.trim(userForm.getUsername()),
						Role.ROLE_ADMIN.name()));
				authority2.setUser(user);

				Authority saveAndFlush2 = authorityRepository
						.saveAndFlush(authority2);

				user.getAuthorities().add(saveAndFlush2);
			}

		}

		userRepository.saveAndFlush(user);

		log(Action.Update, Target.User, user);

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "update2", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_USER" })
	public RespBody update2(@Valid UserForm userForm,
			BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			return RespBody.failed(bindingResult.getAllErrors().stream()
					.map(err -> err.getDefaultMessage())
					.collect(Collectors.joining("<br/>")));
		}

		if (!WebUtil.getUsername().equals(userForm.getUsername())) {
			logger.error("普通用户无权限修改其他用户信息!");
			return RespBody.failed("普通用户无权限修改其他用户信息!");
		}

		User user = userRepository.findOne(userForm.getUsername());

		if (user == null) {
			logger.error("更新用户不存在! ID: {}", userForm.getUsername());
			return RespBody.failed("更新用户不存在!");
		}

		if (StringUtil.isNotEmpty(userForm.getPassword())) {

			if (userForm.getPassword().length() < 6) {
				logger.error("修改密码长度小于六位, ID: {}", userForm.getUsername());
				return RespBody.failed("修改密码长度不能小于六位!");
			}

			user.setPassword(passwordEncoder.encode(userForm.getPassword()));
		}

		if (userForm.getEnabled() != null
				&& user.getStatus() != Status.Bultin) {
			user.setEnabled(userForm.getEnabled());
		}

		if (userForm.getMail() != null) {
			user.setMails(userForm.getMail());
		}

		if (userForm.getName() != null) {
			user.setName(userForm.getName());
		}

		userRepository.saveAndFlush(user);

		log(Action.Update, Target.User, user);

		return RespBody.succeed(user.getUsername());
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@Secured({ "ROLE_ADMIN" })
	public RespBody delete(@RequestParam("username") String username) {

		User user = userRepository.findOne(username);

		if (user == null) {
			logger.error("删除用户不存在! ID: {}", username);
			return RespBody.failed("删除用户不存在!");
		}

		if (user.getStatus() == Status.Bultin) {
			logger.error("内置用户,不能删除! ID: {}", username);
			return RespBody.failed("内置用户,不能删除!");
		}

		userRepository.delete(user);

		log(Action.Delete, Target.User, user);

		return RespBody.succeed(username);
	}

	@RequestMapping(value = "get", method = RequestMethod.GET)
	@ResponseBody
	@Secured({ "ROLE_ADMIN", "ROLE_USER" })
	public RespBody get(@RequestParam("username") String username) {

		User user = userRepository.findOne(username);

		if (user == null) {
			logger.error("查询用户不存在! ID: {}", username);
			return RespBody.failed("查询用户不存在!");
		}

		return RespBody.succeed(user);
	}
}

{'fraction_non_alphanumeric': 0.14946761668401248, 'fraction_numerical': 0.005443919622127932, 'mean_word_length': 4.227662690939527, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718733', 'n_tokens_mistral': 9868, 'n_tokens_neox': 8525, 'n_words': 1364}