cmsprincipal.java

来自「找了很久才找到到源代码」· Java 代码 · 共 490 行 · 第 1/2 页

JAVA
490
字号
     * 
     * @return the principal read from the OpenCms database
     * 
     * @throws CmsException in case the principal could not be read
     */
    public static I_CmsPrincipal readPrincipal(CmsObject cms, String type, String name) throws CmsException {

        if (CmsStringUtil.isNotEmpty(type)) {
            String upperCaseType = type.toUpperCase();
            if (PRINCIPAL_GROUP.equals(upperCaseType)) {
                // this principal is a group
                return cms.readGroup(name);
            } else if (PRINCIPAL_USER.equals(upperCaseType)) {
                // this principal is a user
                return cms.readUser(name);
            }
        }
        // invalid principal type was given
        throw new CmsDbEntryNotFoundException(Messages.get().container(
            Messages.ERR_INVALID_PRINCIPAL_TYPE_2,
            type,
            name));
    }

    /**
     * Utility function to read a principal by its id from the OpenCms database using the 
     * provided OpenCms user context.<p>
     * 
     * @param cms the OpenCms user context to use when reading the principal
     * @param id the id of the principal to read
     * 
     * @return the principal read from the OpenCms database
     * 
     * @throws CmsException in case the principal could not be read
     */
    public static I_CmsPrincipal readPrincipalIncludingHistory(CmsObject cms, CmsUUID id) throws CmsException {

        try {
            // first try to read the principal as a user
            return cms.readUser(id);
        } catch (CmsException exc) {
            // assume user does not exist
        }
        try {
            // now try to read the principal as a group
            return cms.readGroup(id);
        } catch (CmsException exc) {
            //  assume group does not exist
        }
        try {
            // at the end try to read the principal from the history
            return cms.readHistoryPrincipal(id);
        } catch (CmsException exc) {
            //  assume the principal does not exist at all
        }
        // invalid principal name was given
        throw new CmsDbEntryNotFoundException(Messages.get().container(Messages.ERR_INVALID_PRINCIPAL_1, id));
    }

    /**
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Object obj) {

        if ((this == obj) || this.equals(obj)) {
            return 0;
        }
        I_CmsPrincipal that = (I_CmsPrincipal)obj;
        return this.getName().compareTo(that.getName());
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {

        if (obj == this) {
            return true;
        }
        if (obj instanceof I_CmsPrincipal) {
            if (m_id != null) {
                return m_id.equals(((I_CmsPrincipal)obj).getId());
            }
        }
        return false;
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#getDescription()
     */
    public String getDescription() {

        return m_description;
    }

    /**
     * Returns the display name of this principal including the organizational unit.<p>
     * 
     * @param cms the cms context
     * @param locale the locale
     * 
     * @return the display name of this principal including the organizational unit
     * 
     * @throws CmsException if the organizational unit could not be read 
     */
    public String getDisplayName(CmsObject cms, Locale locale) throws CmsException {

        return Messages.get().getBundle(locale).key(
            Messages.GUI_PRINCIPAL_DISPLAY_NAME_2,
            getSimpleName(),
            OpenCms.getOrgUnitManager().readOrganizationalUnit(cms, getOuFqn()).getDisplayName(locale));
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#getFlags()
     */
    public int getFlags() {

        return m_flags;
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#getId()
     */
    public CmsUUID getId() {

        return m_id;
    }

    /**
     * Returns the fully qualified name of this principal.<p>
     *
     * @return the fully qualified name of this principal
     * 
     * @see java.security.Principal#getName()
     */
    public String getName() {

        return m_name;
    }

    /**
     * Returns the fully qualified name of the associated organizational unit.<p>
     *
     * @return the fully qualified name of the associated organizational unit
     */
    public String getOuFqn() {

        return CmsOrganizationalUnit.getParentFqn(m_name);
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#getPrefixedName()
     */
    public String getPrefixedName() {

        if (isUser()) {
            return getPrefixedUser(getName());
        } else if (isGroup()) {
            return getPrefixedGroup(getName());
        }
        return getName();
    }

    /**
     * Returns the simple name of this organizational unit.
     *
     * @return the simple name of this organizational unit.
     */
    public String getSimpleName() {

        return CmsOrganizationalUnit.getSimpleName(m_name);
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {

        if (m_id != null) {
            return m_id.hashCode();
        }
        return CmsUUID.getNullUUID().hashCode();
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#isEnabled()
     */
    public boolean isEnabled() {

        return (getFlags() & I_CmsPrincipal.FLAG_DISABLED) == 0;
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#isGroup()
     */
    public boolean isGroup() {

        return (this instanceof CmsGroup);
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#isUser()
     */
    public boolean isUser() {

        return (this instanceof CmsUser);
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#setDescription(java.lang.String)
     */
    public void setDescription(String description) {

        m_description = description;
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#setEnabled(boolean)
     */
    public void setEnabled(boolean enabled) {

        if (enabled != isEnabled()) {
            // toggle disabled flag if required
            setFlags(getFlags() ^ I_CmsPrincipal.FLAG_DISABLED);
        }
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#setFlags(int)
     */
    public void setFlags(int value) {

        m_flags = value;
    }

    /**
     * @see org.opencms.security.I_CmsPrincipal#setName(java.lang.String)
     */
    public void setName(String name) {

        checkName(CmsOrganizationalUnit.getSimpleName(name));
        m_name = name;
    }
}

⌨️ 快捷键说明

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