liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.basic.x01.system.controller;
 
import javax.annotation.Resource;
 
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.basic.x01.base.BaseController;
import com.basic.x01.helper.CommonHelper;
import com.basic.x01.system.mapper.SystemMapper;
import com.basic.x01.system.model.TSysUser;
 
/**
 * 用户登录
 * 
 * @company 北京贝思科技术有限公司
 * @author liuyajun, 8384503@qq.com
 * @date 2016年1月12日
 * @time 下午3:42:30
 */
 
@Controller
@Transactional(rollbackFor=Throwable.class)
public class Login extends BaseController {
    
    Logger log = Logger.getLogger(getClass());
    
//    @Autowired
//    private UserService userService;
    
    @Resource
    private SystemMapper userMapper;
 
    @RequestMapping(value="login")
    public String login(){
        return "forward:/frame-jsp/login.jsp";
    }
    
    @RequestMapping(value="loginedNoAccess")
    public String loginedNoAccess(){
        return "redirect:/frame-jsp/logined-no-access.jsp";
    }
    
    @RequestMapping(value="logOut")
    public String logOut(){
        this.getRequest().getSession().invalidate();
        return "redirect:/";
    }
    
    @RequestMapping(value="myProfile")
    public String myProfile(
            @Param("option") String option,
            @Param("oldpwd") String oldpwd,
            TSysUser editUser){
 
        if("modify".equals(option)){
            //modify
            TSysUser user = this.getLoingedUser();
 
            if(editUser==null
                    || this.isEmpty(editUser.getUserId())
                    || this.isEmpty(editUser.getRealName())){
                throw this.exception("输入参数错误");
            }
            
            if(! this.isEmpty(oldpwd) 
                    && ! this.isEmpty(editUser.getPassword())
                    && ! user.getPassword().equals(oldpwd)){
                throw this.exception("原密码不正确");
            }
            
            userMapper.updateUser(editUser);
            
            //设置session中存储的对象值
            user.setRealName(editUser.getRealName());
            if(! this.isEmpty(editUser.getPassword())){
                user.setPassword(editUser.getPassword());
            }
 
            return this.ajax(null);
        }
        
        this.getRequest().setAttribute("loginedUser", this.getLoingedUser());
        return "system/my-profile";
    }
 
    @RequestMapping(value="/loginCheckUser", method=RequestMethod.POST)
    //@Transactional(rollbackFor=Throwable.class, propagation=Propagation.NOT_SUPPORTED)
    public String checkUser(TSysUser user) {
        String loginName = user.getLoginName();
        
        user = userMapper.getUserByLogin(user.getLoginName(), user.getPassword());
        
        if(user ==null || ! CommonHelper.STATUS_VALID.equals(user.getStatus())){
            log.info("Logined error: "+loginName);
            throw this.exception("用户名密码错误");
        }
        
        if(user.getOrg()==null 
                || ! CommonHelper.STATUS_VALID.equals(user.getOrg().getStatus())){
            throw this.exception("当前用户组织已挂起");
        }
        
        if(user.getRole()==null
                || ! CommonHelper.STATUS_VALID.equals(user.getRole().getStatus())){
            throw this.exception("无权限登录");
        }
        
        this.getRequest().getSession().setAttribute(
                SESSION_LOGINED_USER_KEY, user);
        
        log.info("Logined: "+user.getLoginName());
        
        return this.ajax(null);
    }
}