⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securityserviceimpl.java

📁 关于 Jaoso新闻文章发布系统 --- --- --- --- --- --- --- --- --- --- --- --- --- -- 版本信息:Jaoso新闻文章发布系统 0.9.1b
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  2004/5/11 created
 *
 */
package jaoso.framework.service.impl;

import jaoso.framework.dao.AccountDAO;
import jaoso.framework.dao.RightDAO;
import jaoso.framework.dao.RoleDAO;

import jaoso.framework.domain.Account;
import jaoso.framework.domain.Person;
import jaoso.framework.domain.Right;
import jaoso.framework.domain.Role;

import jaoso.framework.exception.AccountAlreadyExistException;
import jaoso.framework.exception.BusinessException;
import jaoso.framework.exception.DAOException;
import jaoso.framework.exception.GroupExistException;
import jaoso.framework.exception.RightExistException;

import jaoso.framework.security.Acl;
import jaoso.framework.security.ProtectedResource;
import jaoso.framework.security.impl.SimpleCallbackHandler;

import jaoso.framework.service.SecurityService;

import jaoso.framework.util.MyBeanUtils;
import jaoso.framework.util.MyUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;

import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;


/**
 * @author edgeloner
 */
public class SecurityServiceImpl extends BaseService implements SecurityService {

    //~ Static fields/initializers =============================================

    /** log */
    private static Log log = LogFactory.getLog(SecurityServiceImpl.class);

    //~ Instance fields ========================================================

    /** accountDAO */
    private AccountDAO accountDAO;

    /** box and group mapping collection */
    private Acl acl;

    /**  Right dao */
    private RightDAO rightDAO;

    /**  Role dao */
    private RoleDAO groupDAO;

    //~ Methods ================================================================

    /**
     * DOCUMENT ME!
     *
     * @param dao DOCUMENT ME!
     */
    public final void setAccountDAO(final AccountDAO dao) {

        accountDAO = dao;
    }

    /**
     * DOCUMENT ME!
     *
     * @param a DOCUMENT ME!
     */
    public final void setAcl(final Acl a) {

        this.acl = a;
    }

    /**
     * @param dao group DAO
     */
    public final void setGroupDAO(final RoleDAO dao) {

        this.groupDAO = dao;
    }

    /**
     * @param dao right DAO
     */
    public final void setRightDAO(final RightDAO dao) {

        this.rightDAO = dao;
    }

    /**
    * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#changeAccountGroup(java.io.Serializable, java.io.Serializable)
     */
    public final void changeAccountRole(final Serializable accountId,
        final Serializable groupId) throws BusinessException {

        Account account = findAccountById(accountId);
        Role group = findRoleById(groupId);
        Role ogroup = account.getGroup();

        try {

            if ((ogroup != null)
                    && !MyUtils.isBlank(ogroup.getId())
                    && !ogroup.getId()
                                  .equals(group.getId())) {

                ogroup.getAccounts()
                      .remove(account);
                account.setGroup(group);
                group.getAccounts()
                     .add(account);
                groupDAO.updateGroup(ogroup);
            } else if ((ogroup == null) || MyUtils.isBlank(ogroup.getId())) {

                account.setGroup(group);
                group.getAccounts()
                     .add(account);
            }

            groupDAO.updateGroup(group);
            accountDAO.updateAccount(account);
        } catch (DAOException e) {

            throw new BusinessException(e.getMessage());
        }

        initSecurity();
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#createRight(jaoso.framework.dto.RightDTO)
     */
    public final void createRight(final Right right)
        throws BusinessException, RightExistException {

        if (rightDAO.isExist(right)) {

            throw new RightExistException("right already exist");
        }

        try {

            rightDAO.createRight(right);
        } catch (DAOException e) {

            throw new BusinessException(e.getMessage());
        }

        initSecurity();
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#createGroup(jaoso.framework.dto.GroupDTO)
     */
    public final void createRole(final Role group, final String[] rights)
        throws BusinessException, GroupExistException {

        if (group == null) {

            throw new BusinessException("group cant not be null!");
        }

        if (groupDAO.isExist(group.getDesc())) {

            throw new GroupExistException("group desc already exist!");
        }

        try {

            Set rightset = new HashSet();
            Right right;

            for (int i = 0, n = rights.length; i < n; i++) {

                right = rightDAO.getRight(rights[i]);

                if (right != null) {

                    right.getGroups()
                         .add(group);
                    rightset.add(right);
                    rightDAO.updateRight(right);
                }
            }

            group.setRights(rightset);
            groupDAO.createGroup(group);
        } catch (DAOException e) {

            log.error("create group error: ", e);
            throw new BusinessException(e.getMessage());
        }
    }

    //==========================================================================

    /**
     * (non-Javadoc)
     *
     * @see jaoso.application.service.SecurityService#findAccountById(java.lang.String)
     */
    public final Account findAccountById(final Serializable id) {

        if (MyUtils.isBlank(id)) {

            return null;
        }

        return accountDAO.getAccount(id);
    }

    //==========================================================================

    /**
     * (non-Javadoc)
     *
     * @see jaoso.application.service.SecurityService#findAccountByName(java.lang.String)
     */
    public final Account findAccountByName(final String name)
        throws BusinessException {

        if (MyUtils.isBlank(name)) {

            return null;
        }

        Account account = null;

        try {

            account = accountDAO.getAccountByName(name);
        } catch (DAOException e) {

            log.error("findAccountByName error: " + e);
            throw new BusinessException(e.getMessage());
        }

        return account;
    }

    //==========================================================================

    /**
     * (non-Javadoc)
     *
     * @see jaoso.application.service.SecurityService#findAllAccount()
     */
    public final Account[] findAllAccount() throws BusinessException {

        Account[] entitys = null;

        try {

            entitys = accountDAO.findAllAccount();
        } catch (DAOException e) {

            log.error("findAllAccount error: " + e);
            throw new BusinessException(e.getMessage());
        }

        if (MyUtils.isBlank(entitys)) {

            return null;
        }

        return entitys;
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#findAllGroup()
     */
    public final Role[] findAllGroup() throws BusinessException {

        Role[] entitys = null;

        try {

            entitys = groupDAO.findAllGroup();
        } catch (DAOException e) {

            log.error("findAllAccount error: " + e);
            throw new BusinessException(e.getMessage());
        }

        if (MyUtils.isBlank(entitys)) {

            return null;
        }

        return entitys;
    }

    /**
    * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#findAllRight()
     */
    public final Right[] findAllRight() throws BusinessException {

        Right[] entitys = null;

        try {

            entitys = rightDAO.findAllRight();
        } catch (DAOException e) {

            log.error("findAllAccount error: " + e);
            throw new BusinessException(e.getMessage());
        }

        if (MyUtils.isBlank(entitys)) {

            return null;
        }

        return entitys;
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#findGroupById(java.io.Serializable)
     */
    public final Role findRoleById(final Serializable id)
        throws BusinessException {

        Role group = null;

        try {

            group = groupDAO.getGroup(id);
        } catch (DAOException e) {

            throw new BusinessException(e.getMessage());
        }

        return group;
    }

    //==========================================================================

    /**
     * DOCUMENT ME!
     *
     * @throws BusinessException DOCUMENT ME!
     */
    public final void initSecurity() throws BusinessException {

        Role[] objs = null;

        try {

            objs = groupDAO.findAllGroup();
        } catch (DAOException e) {

            log.error("initSecurity error: " + e);
        }

        //if not found group in database create init data to database
        if (MyUtils.isBlank(objs)) {

            Role group = new Role();
            group.setDesc("Root");

            Role tmpGroup = new Role();
            tmpGroup.setDesc("TEMP");

            Person person = new Person();
            person.setName("root");

            Date date = new Date();
            person.setCreatedate(date);
            person.setLastdate(date);

            Account user = new Account();
            user.setName("root");
            user.setPassword(MyUtils.toMD5("root"));
            user.setGroup(group);
            user.setPerson(person);

            Set rights = new HashSet();
            Set groups = new HashSet();
            groups.add(group);

            ProtectedResource item;

            for (Iterator it = acl.getProtectedResources()
                                  .iterator(); it.hasNext();) {

                item = (ProtectedResource) it.next();

                Right right = new Right();
                right.setBox(item.getBox());
                right.setDesc(item.getDesc());
                right.setUrl(item.getUrl());
                right.setGroups(groups);
                rights.add(right);
                log.info("a right " + right);
            }

            // end for
            group.setRights(rights);

            try {

                accountDAO.createAccount(user);
                groupDAO.createGroup(tmpGroup);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -