package com.lgx8.right.action;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.lgx8.common.PageArgument;
import com.lgx8.common.PageList;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.right.dao.IRoleDao;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.Role;
import com.lgx8.right.entities.RunTimeUser;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IRoleService;
import com.lgx8.right.service.IUserCardService;
/**
* 处理用户授权类
* @author chenbutao
*
*/
@Controller
public class AuthzController extends BaseController {
@Resource(name="userDao")
public IUserDao userDao;
@Resource(name="roleService")
IRoleService roleService;
@Resource(name="roleDao")
IRoleDao roleDao;
@Resource(name="userCardService")
public IUserCardService userCardService;
@RequestMapping(value="/authzUserList.do")
public ModelAndView authzUserList(HttpServletRequest request,HttpServletResponse response){
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
String userName = request.getParameter("username");
PageArgument pageArgment = getPageInfo(request);
String hql = "";
Object[] o = null;
if(rtUser.getUsertype() == 6)
{
hql = "from User u where u.enabled = true and u.id != ? ";
//
hql += " and ( u.parent.id =? or (exists (from Merchant m where m.SJFRDB.id = u.id)) or (exists (from Organization o where o.jgfzr.id = u.id)))";
o = new Object[]{rtUser.getId(),rtUser.getId()};
}else
{
if(null==userName||"".equals(userName.trim())){
hql = "from User u where u.enabled = true and u.parent.id =?";
o = new Object[]{rtUser.getId()};
}else{
hql = "from User u where u.enabled = true and u.truename like ? and u.parent.id = ?";
o = new Object[]{"%"+userName+"%",rtUser.getId()};
}
}
hql += " order by u.registertime desc";
PageList pageList = userDao.findUserByConditions(hql, o, pageArgment);
printJSON(response, pageList.toJSON());
return null;
}
@RequestMapping(value="/authzRoleIni.do")
public ModelAndView authzRoleIni(HttpServletRequest request,HttpServletResponse response){
String uid = request.getParameter("id");
//查询被授权用户
User user = userDao.findUser("id", Long.parseLong(uid));
RunTimeUser curUser = RunTimeUser.getRunTimeUser(request);
List<Role> roleList = roleService.queryAllRoleByUser(curUser.getId());
request.getSession().setAttribute("curAuthzuser", user);
return new ModelAndView("management/Authorize/authzRoleIni")
.addObject("roleList", roleList);
}
@RequestMapping(value="/saveAuthzRole.do")
public ModelAndView saveAuthzRole(HttpServletRequest request,HttpServletResponse response){
String [] roles = request.getParameterValues("authzRole");
User user = (User)request.getSession().getAttribute("curAuthzuser");
Set<Role> roleSet = new HashSet<Role>();
if (roles.length > 0) {
for (int i=0 ; i < roles.length ; i++) {
if (!"".equals(roles[i])) {
Role role = roleDao.findRole("id", Integer.parseInt(roles[i]));
roleSet.add(role);
}
}
}
user.setRoles(roleSet);
userDao.updateUser(user);
request.getSession().removeAttribute("curAuthzuser");
return new ModelAndView(new RedirectView("page/management/Authorize/authzUserList.jsp"));
}
/**
* 下属子帐号添加
* @param request
* @param response
* @return
*/
@RequestMapping(value="/subUser/forsubUser.do")
public ModelAndView forsubUser(HttpServletRequest request,HttpServletResponse response) {
return new ModelAndView("/management/Authorize/updateSubUser");
}
/**
* 保存子帐号信息
* @param request
* @param response
* @return
*/
@RequestMapping(value="/subUser/saveSubUser.do")
public ModelAndView saveSubUser(HttpServletRequest request,HttpServletResponse response) {
String uid = request.getParameter("id");//获取当前用户id 用于修改用户基本信息
String email = request.getParameter("email");
String password = request.getParameter("password");
String truename = request.getParameter("truename");
String sex = request.getParameter("sex");
String mobile = request.getParameter("mobile");
String username = request.getParameter("username");
RunTimeUser rtUser = RunTimeUser.getRunTimeUser(request);
if(rtUser!=null){
Long usertype = rtUser.getUsertype();
if (rtUser.getUsertype() == User.USER_TYPE_GSYG) {
usertype = Long.parseLong(request.getParameter("usertype"));
}
Long orgid = rtUser.getOrganizationid();
Long parenttype = rtUser.getParenttype();
User user = new User();
user.setEmail(email);
user.setPassword(password);
user.setUsertype(usertype);
user.setParenttype(parenttype);
user.setOrganizationid(orgid);
user.setSex(Integer.parseInt(sex));
user.setUsername(username);
user.setTruename(truename);
user.setRegistertime(new Date());
user.setLastlogin(new Date());
user.setMobile(mobile);
user.setEnabled(true);
user.setLocked(false);
user.setCardEnd("0");
user.setCardStart("0");
user.setCurMaxCard("0");
User rUser = userCardService.findUserById(rtUser.getId());
user.setOpener(rUser);
userCardService.createSubUser(user, rUser);
}
return new ModelAndView(new RedirectView("../page/management/Authorize/authzUserList.jsp"));
}
/**
* 删除子帐号
*/
@RequestMapping("/subUser/subUserDel.do")
public ModelAndView delUser(HttpServletRequest request,HttpServletResponse response) {
//准备删除的会员列表
String[] delUserIds = this.getKeyStoreArray(request);
if (delUserIds.length > 0) {
for (int i = 0 ; i < delUserIds.length ; i++) {
userCardService.delUserById(Long.parseLong(delUserIds[i]));
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("../page/management/Authorize/authzUserList.jsp"));
}
@RequestMapping("/subUser/resetUserPassword.do")
public ModelAndView resetUserPassword(HttpServletRequest request,HttpServletResponse response) {
//准备删除的会员列表
String[] delUserIds = this.getKeyStoreArray(request);
String password = request.getParameter("password");
if(password == null && "".equals(password))
{
password = "123456";
}
if (delUserIds.length > 0) {
for (int i = 0 ; i < delUserIds.length ; i++) {
userDao.resetUserPassword(Long.parseLong(delUserIds[i]), password);
}
}
this.clearKeyStore(request);
return new ModelAndView(new RedirectView("../page/management/Authorize/authzUserList.jsp"));
}
}