package cn.updev.Users.Static.FuctionClass;

import cn.updev.Users.Static.UserOrGroupInterface.IUser;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 登录判断
 * 获得登录用户
 * Created by hypo on 15-9-29.
 */
public class Login {
    private IUser user;
    private String passWord;

    public Login(){

    }

    public Login(String email, String passWord) {

        //判断合法E-Mail
        Pattern pattern = Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
        Matcher matcher = pattern.matcher(email);

        if(matcher.matches()){

            // email是用户注册邮箱
            user = null;
        }else{

            // 邮箱非法，不用再去尝试获取用户
            user = null;
        }

        this.passWord = passWord;

        try {
            //密码MD5加密 把注册邮箱作为盐
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update((passWord + user.geteMail()).getBytes());
            passWord = new String(md.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public Boolean judge(){

        if(user == null){
            return false;
        }

        if(passWord.equals(user.getPassWord())){
            setSession();
            return true;
        }

        return false;
    }

    private void setSession(){

        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();

        session.setAttribute("LoingedUser", user);

    }

    public IUser getLoginedUser(){

        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();

        IUser rnt = (IUser) session.getAttribute("LoingedUser");

        return rnt;
    }
}

@@ -38,16 +38,78 @@
 Class;%0A%0A
+import cn.updev.Users.Static.UserOrGroupDAO.UserOrGroupQuery;%0A
 import c
@@ -204,16 +204,47 @@
 Context;
+%0Aimport sun.misc.BASE64Encoder;
 %0A%0Aimport
@@ -318,24 +318,69 @@
 ttpSession;%0A
+import java.io.UnsupportedEncodingException;%0A
 import java.
@@ -993,35 +993,121 @@
             
-user = null
+UserOrGroupQuery query = new UserOrGroupQuery();%0A            user = query.queryUserByEMail(email)
 ;%0A        %7De
@@ -1191,13 +1191,8 @@
     
-this.
 pass
@@ -1202,24 +1202,32 @@
 d = passWord
+ + email
 ;%0A%0A        t
@@ -1229,73 +1229,79 @@
     
-try %7B%0A            //%E5%AF%86%E7%A0%81MD5%E5%8A%A0%E5%AF%86 %E6%8A%8A%E6%B3%A8%E5%86%8C%E9%82%AE%E7%AE%B1%E4%BD%9C%E4%B8%BA%E7%9B%90%0A            MessageDigest
+//%E7%A1%AE%E5%AE%9A%E8%AE%A1%E7%AE%97%E6%96%B9%E6%B3%95%0A        MessageDigest md5= null;%0A        try %7B%0A           
  md
+5
  = M
@@ -1345,108 +1345,227 @@
     
-    md.update((passWord + user.geteMail()).getBytes());%0A            passWord = new String
+%7D catch (NoSuchAlgorithmException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        //%E5%8A%A0%E5%AF%86%E5%90%8E%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%0A        BASE64Encoder base64en = new BASE64Encoder();%0A        try %7B%0A            passWord = base64en.encode
 (md
+5
 .digest(
 ));%0A
@@ -1560,16 +1560,42 @@
 .digest(
+passWord.getBytes(%22utf-8%22)
 ));%0A    
@@ -1603,39 +1603,43 @@
    %7D catch (
-NoSuchAlgorithm
+UnsupportedEncoding
 Exception e)
@@ -1683,16 +1683,51 @@
        %7D
+%0A%0A        this.passWord = passWord;
 %0A    %7D%0A%0A
@@ -2426,11 +2426,406 @@
 t;%0A    %7D
+%0A%0A    public void logout()%7B%0A        HttpServletRequest request = ServletActionContext.getRequest();%0A        HttpSession session = request.getSession();%0A%0A        session.setAttribute(%22LoingedUser%22, null);%0A    %7D%0A%0A    public Boolean isNotLogined()%7B%0A%0A        IUser user = getLoginedUser();%0A%0A        if(user == null)%7B%0A            return true;%0A        %7Delse %7B%0A            return false;%0A        %7D%0A    %7D
 %0A%7D%0A

update Login