SysUserController.java 8.4 KB
package com.air.controller;

import java.util.ArrayList;
import java.util.List;

import com.air.config.AbsController;
import com.air.model.Agent;
import com.air.model.StationMap;
import com.air.model.SysLoginLog;
import com.air.model.SysMenu;
import com.air.model.SysRole;
import com.air.model.SysRoleType;
import com.air.model.SysUser;
import com.google.gson.Gson;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.teplot.common.Encrypt;
import com.teplot.common.Response;

/**
 * Depiction:后台用户管理
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date:2016年6月3日 下午11:02:05
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
public class SysUserController extends AbsController {
	private static final String VERIFY_CODE = "verifyCode";

	@Clear
	public void logout() {
		if (getLoginUser() != null) {
			SysLoginLog.dao.log(getLoginUser().getId(), false);

			setSessionAttr("user", null);
		}

		redirect("/");
	}

	// 用户登录
	@Clear
	public void login() {
		Response ret = new Response(CODE_SUCCESS);

		if (!validateCaptcha(VERIFY_CODE)) {
			ret = new Response(VERIFY_CODE_ERROR);
			renderJson(ret);
			return;
		}

		String username = this.getPara("username");
		String password = this.getPara("password");

		SysUser sysUser = SysUser.dao.searchFirst("username", username);

		if (sysUser == null) {
			ret = new Response(CODE_FAILURE);
			ret.setMsg("用户不存在");
			renderJson(ret);
		} else if (!sysUser.getStr("password").equalsIgnoreCase(Encrypt.encrypt(username, password))) {
			ret = new Response(CODE_FAILURE);
			ret.setMsg("密码不正确");
			renderJson(ret);
		} else {
			SysLoginLog.dao.log(sysUser.getId(), true);
			int role = sysUser.getRoleLevel();
			if (role != SysRoleType.SUPER.ordinal() && role != SysRoleType.AGENT.ordinal()) {
				String stationArea = sysUser.getStationArea();
				StationMap station = StationMap.dao.searchFirst("areaCode", stationArea);
				sysUser.put("station", station);
			}
			setSessionAttr("user", sysUser);
			ret = new Response(CODE_SUCCESS);
			renderJson(ret);
		}
	}

	@Clear
	public void doLogin() {
		Response ret = new Response(CODE_FAILURE);
		String url = "http://10.5.13.25/services/rest/token/verifySAMLResponse";
		String SAMLResponse = getPara("SAMLResponse");
		String providerId = getPara("providerId");
		String param = "SAMLResponse=" + SAMLResponse + "&providerId=" + providerId;
		String json = HttpKit.post(url, param);
		Gson gson = new Gson();
		Kv map = gson.fromJson(json, Kv.class);
		if (map != null) {
			String status = map.getStr("status");
			if ("0x0000".equalsIgnoreCase(status)) {
				// 成功获取账号密码
				String username = map.getStr("appLoginID");
				String password = HashKit.sha256(map.getStr("appLoginPass"));
				SysUser sysUser = SysUser.dao.searchFirst("username", username);
				if (sysUser == null) {
					ret.setMsg("用户不存在");
					renderJson(ret);
				} else if (!sysUser.getStr("password").equalsIgnoreCase(Encrypt.encrypt(username, password))) {
					ret = new Response(CODE_FAILURE);
					ret.setMsg("密码不正确");
					renderJson(ret);
				} else {
					//登录成功
					int role = sysUser.getRoleLevel();
					if (role != SysRoleType.SUPER.ordinal() && role != SysRoleType.AGENT.ordinal()) {
						String stationArea = sysUser.getStationArea();
						StationMap station = StationMap.dao.searchFirst("areaCode", stationArea);
						sysUser.put("station", station);
					}
					setSessionAttr("user", sysUser);
					setAttr("loginName", sysUser.getStr("realName"));
					setAttr("menuList", SysMenu.dao.getMenus(getRole()));
					render("index.html");
					return;
				}

			} else {
				ret.setMsg("登录失败");
			}
		} else {
			ret.setMsg("账号密码不能为空");
		}

		renderJson(ret);
	}

	public void list() {
		String key = getPara("key");
		int page = this.getParaToInt("page", 1);
		int pageNum = this.getParaToInt("pageNum", 10);
		Page<SysUser> pageData = SysUser.dao.search(page, pageNum, key, getRole().getLevel(), getStationArea());
		setAttr("dataList", pageData.getList());
		setAttr("curPage", pageData.getPageNumber());
		setAttr("totalSize", pageData.getTotalRow());
		setAttr("totalPage", pageData.getTotalPage());
		setAttr("searchKey", key);
		render("sysUserList.html");
	}

	public void edit() {
		Integer staffId = getParaToInt("staffId");
		SysUser model = SysUser.dao.findById(staffId);
		setAttr("sysUser", model);
		setAttr("station", model != null ? model.get("station") : null);

		List<SysRole> roleList = SysRole.dao.list(getRole().getLevel());
		setAttr("roleList", roleList);

		setAttr("agentList", Agent.dao.searchAll());
		List<StationMap> stationList = null;
		if (getRole().getLevel() == 1) {
			// 超级管理员,显示所有货站
			stationList = StationMap.dao.searchAll();
			StationMap sm = new StationMap();
			sm.setName("选择货站");
			sm.setAreaCode("");
		} else {
			// 普通管理员或者员工,仅显示所属的货站
			stationList = new ArrayList<StationMap>();
			String areaCode = getLoginUser().getStationArea();
			StationMap sm = StationMap.dao.searchFirst("areaCode", areaCode);
			stationList.add(sm);
		}
		setAttr("stationList", stationList);

		render("sysUserEdit.html");
	}

	public void submit() {
		SysUser model = getModel(SysUser.class, "sysUser");
		String msg = "操作成功";
		SysUser staffTemp = SysUser.dao.searchFirst("username", model.getStr("username"));
		Integer staffId = model.getInt("id");
		Integer roleLevel = model.getInt("roleLevel");

		if (staffId != null && SysUser.dao.findById(staffId) != null) {
			// 更新资料
			if (staffTemp != null && staffId != staffTemp.getInt("id")) {
				msg = "该账号已经存在,请更换";
			} else {
				// 账号没有重复
				String password = HashKit.sha256(model.getStr("password"));
				password = Encrypt.encrypt(model.getStr("username"), password);
				model.set("password", password);

				if (roleLevel != null) {
					model.set("job", getJob(roleLevel));
				}

				if (!model.update()) {
					msg = "操作失败";
				}
			}
		} else {
			// 新建员工
			if (staffTemp == null) {
				String password = HashKit.sha256(model.getStr("password"));
				password = Encrypt.encrypt(model.getStr("username"), password);
				model.set("password", password);

				if (roleLevel != null) {
					model.set("job", getJob(roleLevel));
				}

				if (!model.save()) {
					msg = "操作失败";
				}
			} else {
				msg = "该账号已经存在,请更换其它账号";
			}
		}

		if (!msg.equalsIgnoreCase("操作成功")) {
			setAttr("staff", model);
		}

		setAttr("flag", msg);
		render("sysUserEdit.html");
	}

	private String getJob(int roleLevel) {
		SysRole role = SysRole.dao.searchFirst("level", roleLevel);
		if (role != null) {
			return role.getName();
		}
		return "Unknown";
	}

	public void modifyPassword() {
		render("modifyPassword.html");
	}

	public void modifyPasswordAction() {
		String msg = "操作成功";
		String paramOldPassword = getPara("oldPassword");
		String password1 = getPara("password1");
		String password2 = getPara("password2");

		if (!password1.equalsIgnoreCase(password2)) {
			// 密码不一致
			msg = "新密码不一致!";
		} else {
			// 密码一致
			if (password2.length() < 6) {
				// 密码太短
				msg = "密码太短,长度不能小于6位!";
			} else {
				SysUser loginUser = getLoginUser();
				String username = loginUser.getStr("username");
				String oldPassword = Encrypt.encrypt(username, HashKit.sha256(paramOldPassword));
				if (!oldPassword.equals(loginUser.getStr("password"))) {
					msg = "旧密码错误";
				} else {
					loginUser.set("password", Encrypt.encrypt(username, HashKit.sha256(password1)));
					if (!loginUser.update()) {
						msg = "操作失败";
					}
				}
			}
		}

		setAttr("flag", msg);

		if (!msg.equalsIgnoreCase("操作成功")) {
			setAttr("oldPassword", paramOldPassword);
			setAttr("password1", password1);
			setAttr("password2", password2);

			modifyPassword();
		} else {
			setSessionAttr("user", null);
			redirect("/login");
		}
	}

	public void delete() {
		Integer staffId = getParaToInt("staffId", 0);
		SysUser staff = SysUser.dao.findById(staffId);
		if (staff != null) {
			if (staff.delete()) {
				setAttr("flag", "操作成功");
			} else {
				setAttr("flag", "操作失败");
			}
		} else {
			setAttr("flag", "该账号不存在");
		}

		list();
	}
}