001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.tools.accounts;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.workplace.list.A_CmsListDialog;
033import org.opencms.workplace.list.CmsListDefaultAction;
034
035import java.util.List;
036
037/**
038 * Show diferent states depending on user direct/indirect group assignment.<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsGroupStateAction extends CmsListDefaultAction {
043
044    /** The cms context. */
045    private CmsObject m_cms;
046
047    /** Direct group flag. */
048    private final boolean m_direct;
049
050    /** Current user name. */
051    private String m_userName;
052
053    /**
054     * Default constructor.<p>
055     *
056     * @param id the id of the action
057     * @param direct the direct group flag
058     */
059    public CmsGroupStateAction(String id, boolean direct) {
060
061        super(id);
062        m_direct = direct;
063    }
064
065    /**
066     * Default constructor.<p>
067     *
068     * @param id the id of the action
069     * @param cms the cms context
070     * @param direct the direct group flag
071     *
072     * @Deprecated cms object no longer needed
073     */
074    public CmsGroupStateAction(String id, CmsObject cms, boolean direct) {
075
076        super(id);
077        m_cms = cms;
078        m_direct = direct;
079    }
080
081    /**
082     * Returns the cms context.<p>
083     *
084     * @return the cms context
085     */
086    public CmsObject getCms() {
087
088        return m_cms;
089    }
090
091    /**
092     * Returns the user Name.<p>
093     *
094     * @return the user Name
095     */
096    public String getUserName() {
097
098        if (m_userName == null) {
099            m_userName = ((A_CmsUserGroupsList)getWp()).getParamUsername();
100        }
101        return m_userName;
102    }
103
104    /**
105     * Returns the direct group flag.<p>
106     *
107     * @return the direct group flag
108     */
109    public boolean isDirect() {
110
111        return m_direct;
112    }
113
114    /**
115     * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
116     */
117    @Override
118    public boolean isVisible() {
119
120        try {
121            String groupName = (String)getItem().get(A_CmsUserGroupsList.LIST_COLUMN_NAME);
122            List<CmsGroup> dGroups = getCms().getGroupsOfUser(getUserName(), true);
123            CmsGroup group = getCms().readGroup(groupName);
124            if (isDirect()) {
125                return dGroups.contains(group);
126            } else {
127                return !dGroups.contains(group);
128            }
129        } catch (Exception e) {
130            return false;
131        }
132    }
133
134    /**
135     * Sets the user Name.<p>
136     *
137     * @param userName the user Name to set
138     */
139    public void setUserName(String userName) {
140
141        m_userName = userName;
142    }
143
144    /**
145     * @see org.opencms.workplace.list.I_CmsListAction#setWp(org.opencms.workplace.list.A_CmsListDialog)
146     */
147    @Override
148    public void setWp(A_CmsListDialog wp) {
149
150        super.setWp(wp);
151        m_cms = wp.getCms();
152        m_userName = null;
153    }
154}