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

📄 securityserviceimpl.java

📁 关于 Jaoso新闻文章发布系统 --- --- --- --- --- --- --- --- --- --- --- --- --- -- 版本信息:Jaoso新闻文章发布系统 0.9.1b
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            } catch (DAOException e) {

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

        //end if
        //load all rights to acl
        acl.setProtectedResourcesMap(loadRights());
        log.info("*****************init security success!***************");
    }

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

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

        Account account = null;

        try {

            // let the LoginContext instantiate a new Subject
            LoginContext lc = new LoginContext("JaosoLogin",
                    new SimpleCallbackHandler(name, MyUtils.toMD5(password)));
            lc.login();
        } catch (LoginException e) {

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

        //end try-catch
        account = findAccountByName(name);

        return account;
    }

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

    /**
     * DOCUMENT ME!
     *
     * @param account DOCUMENT ME!
     *
     * @throws BusinessException DOCUMENT ME!
     * @throws AccountAlreadyExistException DOCUMENT ME!
     */
    public final void register(final Account account)
        throws BusinessException, AccountAlreadyExistException {

        if (account == null) {

            throw new BusinessException("account can not be null");
        }

        if (accountDAO.isExist(account.getName())) {

            throw new AccountAlreadyExistException("account already exist!");
        }

        account.setPassword(MyUtils.toMD5(account.getPassword()));

        Date date = new Date();
        account.getPerson()
               .setCreatedate(date);
        account.getPerson()
               .setLastdate(date);
        account.getPerson().setCreatedate(date);
        account.getPerson().setLastdate(date);
        try {

            accountDAO.createAccount(account);
        } catch (DAOException e) {

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

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

        if (id == null) {

            throw new BusinessException("accounts can't be null");
        }

        try {

            accountDAO.removeAccount(id);
        } catch (DAOException e) {

            log.error("update Account error: ", e);
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#removeAccount(java.io.Serializable[])
     */
    public final void removeAccount(final Serializable[] ids)
        throws BusinessException {

        if (ids == null) {

            throw new BusinessException("accounts can't be null");
        }

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

            removeAccount(ids[i]);
        }

        initSecurity();
    }

    /**
    * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#removeRight(java.io.Serializable[])
     */
    public final void removeRight(final Serializable[] ids)
        throws BusinessException {

        if (MyUtils.isBlank(ids)) {

            throw new BusinessException("rights can't be null");
        }

        try {

            rightDAO.removeRight(ids);
        } catch (DAOException e) {

            log.error("remove right error: ", e);
            throw new BusinessException(e.getMessage());
        }

        initSecurity();
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#removeGroup(java.io.Serializable[])
     */
    public final void removeRole(final Serializable[] ids)
        throws BusinessException {

        if (MyUtils.isBlank(ids)) {

            throw new BusinessException("groups can't be null");
        }

        try {

            groupDAO.removeGroup(ids);
        } catch (DAOException e) {

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

        initSecurity();
    }

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

    /**
     * (non-Javadoc)
     *
     * @see jaoso.application.service.SecurityService#updateAccount(jaoso.application.dto.AccountDTO)
     */
    public final void updateAccount(final Account account)
        throws BusinessException, AccountAlreadyExistException {

        if ((account == null) || MyUtils.isBlank(account.getId())) {

            throw new BusinessException("account can't not be null");
        }

        if (accountDAO.isExist(account.getName(), account.getId())) {

            throw new AccountAlreadyExistException("account already exist!");
        }

        Account entity = null;

        try {

            entity = accountDAO.getAccount(account.getId());

            if (entity == null) {

                throw new BusinessException("account not found ");
            }

            Person person = entity.getPerson();
            MyBeanUtils.copyProperties(person, account.getPerson());
            person.setLastdate(new Date());
            account.setPerson(person);
            MyBeanUtils.copyProperties(entity, account);

            //convert apssword to MD5
            entity.setPassword(MyUtils.toMD5(entity.getPassword()));
            accountDAO.updateAccount(entity);
        } catch (DAOException e) {

            log.error("update Account error: ", e);
            throw new BusinessException(e.getMessage());
        } catch (IllegalAccessException e) {

            log.error("update Account error: ", e);
            throw new BusinessException(e.getMessage());
        } catch (InvocationTargetException e) {

            log.error("update Account error: ", e);
            throw new BusinessException(e.getMessage());
        }
    }

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

        if (right == null) {

            return;
        }

        if (rightDAO.isExist4u(right)) {

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

        try {

            Right entity = rightDAO.getRight(right.getId());

            // entity.setBox(right.getBox());
            // entity.setDesc(right.getDesc());
            // entity.setUrl(right.getUrl());
            MyBeanUtils.copyProperties(entity, right);
            rightDAO.updateRight(entity);
        } catch (DAOException e) {

            log.error("update right error: ", e);
            throw new BusinessException(e.getMessage());
        } catch (IllegalAccessException e) {

            log.error("update right error: ", e);
            throw new BusinessException(e.getMessage());
        } catch (InvocationTargetException e) {

            log.error("update right error: ", e);
            throw new BusinessException(e.getMessage());
        }
    }

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

        if (MyUtils.isBlank(rights)) {

            return;
        }

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

            updateRight(rights[i]);
        }

        initSecurity();
    }

    /**
     * (non-Javadoc)
     * @see jaoso.framework.service.SecurityService#updateGroup(jaoso.framework.domain.Role, java.lang.String[], java.lang.String[])
     */
    public final void updateRole(final Role group, final String[] removeRight,
        final String[] addRight) throws BusinessException, GroupExistException {

        if ((group == null) || MyUtils.isBlank(group.getId())) {

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

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

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

        Role entity = findRoleById(group.getId());

        if (entity == null) {

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

        if (!MyUtils.isBlank(group.getDesc())) {

            entity.setDesc(group.getDesc());
        }

        Right right = null;

        //remove right
        if (!MyUtils.isBlank(removeRight)) {

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

                right = rightDAO.getRight(removeRight[i]);
                right.getGroups()
                     .remove(entity);
                entity.getRights()
                      .remove(right);
                rightDAO.updateRight(right);
            }
        }

        //add right
        if (!MyUtils.isBlank(addRight)) {

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

                right = rightDAO.getRight(addRight[i]);
                right.getGroups()
                     .add(entity);
                entity.getRights()
                      .add(right);
                rightDAO.updateRight(right);
            }
        }

        groupDAO.updateGroup(entity);
    }

    /**
     * load account by id
     *
     * @param id account id
     *
     * @return account
     *
     * @throws BusinessException any error
     */
    private Account getAccount(final Serializable id) throws BusinessException {

        Account entity = null;

        try {

            entity = (Account) getQueryManager()
                                   .findByPK(new Account(), id);
        } catch (DAOException e) {

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

        return entity;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    private Map loadRights() {

        Map rightMap = new HashMap();
        Right[] rights = null;

        try {

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

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

        Right right;
        Set groups;
        Role group;
        Collection protectedResources = new ArrayList();

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

            right = rights[i];
            groups = right.getGroups();

            Map groupMap = new HashMap();

            if (!MyUtils.isBlank(groups)) {

                for (Iterator it = groups.iterator(); it.hasNext();) {

                    String groupId = ((Role) it
                                      .next()).getId()
                                      .trim();
                    groupMap.put(groupId, groupId);
                }
            }

            protectedResources.add(new ProtectedResource(right.getBox(),
                    right.getDesc(), right.getUrl()));
            rightMap.put(right.getUrl(), groupMap);
        }

        rightMap.put("protectedResources", protectedResources);

        return rightMap;
    }
}

//end class

⌨️ 快捷键说明

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