SSOStubPersonAttributeDao.java 3.53 KB
package weaver.authentication.dao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jasig.services.persondir.IPersonAttributes;
import org.jasig.services.persondir.support.AttributeNamedPersonImpl;
import org.jasig.services.persondir.support.StubPersonAttributeDao;
import org.springframework.util.LinkedCaseInsensitiveMap;

import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by crazyDream on 2018/9/17.
 */
public class SSOStubPersonAttributeDao extends StubPersonAttributeDao {
    @NotNull
    private SSOUserDaoJdbc ssoUserDaoJdbc;

    @NotNull
    private String userNameAttribute;

    public void setSsoUserDaoJdbc(SSOUserDaoJdbc ssoUserDaoJdbc) {
        this.ssoUserDaoJdbc = ssoUserDaoJdbc;
    }

    public void setUserNameAttribute(String userNameAttribute) {this.userNameAttribute = userNameAttribute;}
    @Override
    public IPersonAttributes getPerson(String uid) {


        Map<String, List<Object>> attributes = new HashMap<String, List<Object>>();
        try {
            Map<String,Object> user = ssoUserDaoJdbc.getSSOUserByUsernameMap(uid);
            List<Object> relatedAccount = (List<Object>)ssoUserDaoJdbc.getRelatedAccountByUsername(uid);

            //用户密码正确且用户未锁定,封装返回参数
            attributes.put("id", Collections.singletonList(user.get("id")));
            attributes.put("loginid",Collections.singletonList(user.get("loginid")));
            attributes.put("lastname", Collections.singletonList(user.get("lastname")));
            attributes.put("sex", Collections.singletonList(user.get("sex")));
            attributes.put("birthday", Collections.singletonList(user.get("birthday")));
            attributes.put("systemlanguage", Collections.singletonList(user.get("systemlanguage")));
            attributes.put("telephone", Collections.singletonList(user.get("telephone")));
            attributes.put("mobile", Collections.singletonList(user.get("mobile")));
            attributes.put("email", Collections.singletonList(user.get("email")));
            attributes.put("departmentid", Collections.singletonList(user.get("departmentid")));
            attributes.put("subcompanyid", Collections.singletonList(user.get("subcompanyid1")));
            attributes.put("managerid", Collections.singletonList(user.get("managerid")));
            attributes.put("assistantid", Collections.singletonList(user.get("assistantid")));
            attributes.put("workcode", Collections.singletonList(user.get("workcode")));
            attributes.put("status", Collections.singletonList(user.get("status")));
            attributes.put("countryid", Collections.singletonList(user.get("countryid")));

            attributes.put("password", Collections.singletonList(user.get("password")));

            //用户关联账号信息,转化一下
            if (relatedAccount!=null&&relatedAccount.size()>0) {
                JSONArray jsonArray = new JSONArray();
                for (Object o : relatedAccount) {
                    LinkedCaseInsensitiveMap linkedCaseInsensitiveMap = (LinkedCaseInsensitiveMap) o;
                    jsonArray.add(JSONObject.fromObject(linkedCaseInsensitiveMap));
                }
                attributes.put("relatedAccount", Collections.<Object>singletonList(jsonArray.toString()));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AttributeNamedPersonImpl(this.userNameAttribute,attributes);

    }
}