Package com.wizriver.web.actions

Source Code of com.wizriver.web.actions.SsUserAction

package com.wizriver.web.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.wizriver.config.SpringSecurityUtils;
import com.wizriver.entity.beans.VgRole;
import com.wizriver.entity.beans.VgUser;
import com.wizriver.service.SsUserManager;
import com.wizriver.utils.HibernateWebUtils;
import com.wizriver.utils.MD5String;
import com.wizriver.utils.WPage;

@SuppressWarnings("serial")
@Namespace("/admin")
@Controller
@Results({@Result(name="admin",location="admin_main.jsp"),
    @Result(name="viewadmin",location="admin_view.jsp")})
public class SsUserAction extends ActionSupport implements Preparable {
 
    private SsUserManager ssUserManager;
//    private List ssUsers;
   
  //-- 页面属性 --//
    private Long id;
    private VgUser user;
    private WPage<VgUser> page = new WPage<VgUser>(2);
  private List<Long> checkedRoleIds; //页面中钩选的角色id列表
 
 
    private String newPwd1;
    private String newPwd2;
    MD5String md5 = new MD5String();
    private String wantPwd;
    private String loginName;

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getNewPwd1() {
        return newPwd1;
    }

    public void setNewPwd1(String newPwd1) {
        this.newPwd1 = newPwd1;
    }

    public String getNewPwd2() {
        return newPwd2;
    }

    public void setNewPwd2(String newPwd2) {
        this.newPwd2 = newPwd2;
    }

    public String getWantPwd() {
        return wantPwd;
    }

    public void setWantPwd(String wantPwd) {
        this.wantPwd = wantPwd;
    }

    public void setSsUserManager(SsUserManager ssUserManager) {
        this.ssUserManager = ssUserManager;
    }

  //-- ModelDriven 与 Preparable函数 --//
    public void setId(Long  id) {
        this. id =  id;
    }

  public Long getId() {
    return id;
  }

  public VgUser getUser() {
    return user;
  }

  public void setUser(VgUser user) {
    this.user = user;
  }
 
  //-- 页面属性访问函数 --//
  /**
   * list页面显示用户分页列表.
   */
  public WPage<VgUser> getPage() {
    return page;
  }
 
    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (ServletActionContext.getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String ssUserId = ServletActionContext.getRequest().getParameter("id");
            if (ssUserId != null && !ssUserId.equals("")) {
              user = ssUserManager.get(new Long(ssUserId));
            }
        }
    }

    public String ssList(){
//    List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
//    //设置默认排序方式
//    if (!page.isOrderBySetted()) {
//      page.setOrderBy("id");
//      page.setOrder(Page.ASC);
//    }
//    page.setAutoCount(true);
//    page.setPageSize(20);
//   
//    page = ssUserManager.searchUser(page, filters);
      page = ssUserManager.findVguser(page, null);
    return "admin-user";
  }   

  public String input() throws Exception {
    if (id != null) {
      user = ssUserManager.get(id);
    } else {
      user = new VgUser();
    }
    checkedRoleIds = user.getRoleIds();
    return "UserInput";
  }
 
  /**
   * input页面显示用户拥有的角色.
   */
  public List<Long> getCheckedRoleIds() {
    return checkedRoleIds;
  }

  /**
   * input页面显示所有角色列表.
   */
  public List<VgRole> getAllRoleList() {
    return ssUserManager.getAllRole();
  }
 
  /**
   * input页面提交用户拥有的角色.
   */
  public void setCheckedRoleIds(List<Long> checkedRoleIds) {
    this.checkedRoleIds = checkedRoleIds;
  }

    public String delete() {
    try {
            String ssUserId = ServletActionContext.getRequest().getParameter("id");
            if (ssUserId != null && !ssUserId.equals("")) {
              user = ssUserManager.get(new Long(ssUserId));
            }
           
      ssUserManager.remove(user.getId());
      addActionMessage("删除用户成功");
    } catch (Exception e) {
      addActionMessage("删除用户失败");
    }
        return this.ssList();
    }

    public String edit() {
        if (id != null) {
          user = ssUserManager.get(id);
        } else {
          user = new VgUser();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
    //根据页面上的checkbox选择 整合User的Roles Set
    HibernateWebUtils.mergeByCheckedIds(user.getRoleList(), checkedRoleIds, VgRole.class);
    if(id!=null){
        user = this.ssUserManager.load(VgUser.class, id);
    }else{
         user.setUserType(2l);//后台管理帐户标识 1:前台 2后台
           user.setKeyString(createRandom(4));
           user.setLevel(3601l);
           user.setCreateDate(Calendar.getInstance().getTime());
           user.setState(301l);
           user.setPhoto("http://photo.vegaga.com/dev/images/headimages/headImg_1.jpg");
    }
//    if(null == user){
//        user.setKeyString(createRandom(4));
//          user.setPassword(md5.MD5Encode(user.getPassword() + user.getKeyString()));
//          user.setLevel(3601l);
//          user.setCreateDate(Calendar.getInstance().getTime());
//          user.setUserType(1l);
//          user.setState(301l);
//          user.setPhoto("http://photo.vegaga.com/dev/images/headimages/headImg_1.jpg");
//    }else
//    user.setPassword(md5.MD5Encode(user.getPassword() + user.getKeyString()));
    ssUserManager.save(user);
    addActionMessage("保存用户成功");
    return this.ssList();
    }
   
    public String createRandom(int size) {
        Random rand = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(rand.nextInt(9));
        }
        return sb.toString();
    }
 
  /**
   * 支持使用Jquery.validate Ajax检验用户名是否重复.
   */
  public String checkLoginName() {
    HttpServletRequest request = ServletActionContext.getRequest();
    String newLoginName = request.getParameter("loginName");
    String oldLoginName = request.getParameter("oldLoginName");

    if (ssUserManager.isLoginNameUnique(newLoginName, oldLoginName)) {
      Struts2Utils.renderText("true");
    } else {
      Struts2Utils.renderText("false");
    }
    //因为直接输出内容而不经过jsp,因此返回null.
    return null;
 
 
  /**
   * Testing whether the <code>LoginName</code> already exists    
   * @author:<a href="mailto:bobo2581@gmail.com">bobo</a>
   * @throws IOException
   */
  public String checkLoginNameExist() throws IOException{
      VgUser vu =null;
      try {
         vu = ssUserManager.findUserByLoginName(loginName);
        } catch (Exception e) {
            e.printStackTrace();
        }
     
      JSONObject json = new JSONObject();
      json.put("exist", null == vu ? "0":"1");
      PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(json);
      return null;
  }
 
 
     /**
     * Check whether the want to modify the password and original password consistent
     * @author <a href="bobo2581@gmail.com">bobo</a>
     * @throws IOException
     * */
    public String checkPwd() throws IOException{
        String flag = reCheckPwd()?"0":"1"; //0:OK 1:NO
        JSONObject json = new JSONObject();
        json.put("flag", flag);
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(json);
        return null;
    }
   
    /**
     * Backstage again confirmed once, avoid user bypass javascript verification
     * @author <a href="bobo2581@gmail.com">bobo</a>
     * @throws IOException
     * */
    public String changePwd() throws IOException{
        boolean t = true;
        Pattern p = Pattern.compile("[0-9a-zA-Z_-]{6,16}");
        Matcher m1 = p.matcher(newPwd1);
        Matcher m2 = p.matcher(newPwd2);
            t = t && m1.matches() && m2.matches();
       
        t = t && reCheckPwd();
        t = t && (newPwd1.equals(newPwd2));
        if(t){
            VgUser user = this.ssUserManager.getUserByName(SpringSecurityUtils.getCurrentUserName());
//            newPwd1 += user.getKeyString();
//            newPwd1 = md5.MD5Encode(newPwd1);
            user.setPassword(newPwd1);
            this.ssUserManager.store(user);
        }else{
            t = false;
        }
        String flag = t?"0":"1"; //0:OK 1:NO
        JSONObject json = new JSONObject();
        json.put("flag", flag);
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(json);
        return null;
    }
   
    /**
     * Verify password preprocessing
     * @author <a href="bobo2581@gmail.com">bobo</a>
     * */
    public boolean reCheckPwd(){
        VgUser user = this.ssUserManager.getUserByName(SpringSecurityUtils.getCurrentUserName());
        String oldPwd = user.getPassword();
        boolean t = true;
        t = wantPwd!=null?wantPwd.equals(oldPwd)?true:false:false;
        return t;
    }
   
    public String changePassWord(){
        return "showPwd";
    }
}
TOP

Related Classes of com.wizriver.web.actions.SsUserAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.