001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Sun Seng David TAN <stan@nuxeo.com>
016 *     Antoine Taillefer
017 */
018package org.nuxeo.functionaltests.pages.tabs;
019
020import java.util.List;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.functionaltests.Required;
025import org.nuxeo.functionaltests.forms.Select2WidgetElement;
026import org.nuxeo.functionaltests.pages.AbstractPage;
027import org.openqa.selenium.By;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030import org.openqa.selenium.support.FindBy;
031import org.openqa.selenium.support.FindBys;
032import org.openqa.selenium.support.pagefactory.ByChained;
033import org.openqa.selenium.support.ui.Select;
034
035/**
036 * @author Sun Seng David TAN <stan@nuxeo.com>
037 * @deprecated since 7.10. Use {@link PermissionsSubPage} instead.
038 */
039@Deprecated
040public class AccessRightsSubPage extends AbstractPage {
041
042    private static final Log log = LogFactory.getLog(AccessRightsSubPage.class);
043
044    /*
045     * @Required
046     * @FindBy(id = "add_rights_form:nxl_user_group_suggestion:nxw_selection_suggest") WebElement
047     * userSelectionSuggestInputText;
048     */
049
050    @Required
051    @FindBy(id = "add_rights_form:rights_permission_select")
052    WebElement selectPermissionElement;
053
054    @Required
055    @FindBy(id = "add_rights_form:rights_add_button")
056    WebElement addButton;
057
058    @FindBy(id = "validate_rights:document_rights_validate_button")
059    WebElement validateButton;
060
061    @Required
062    @FindBys({ @FindBy(id = "block_inherit"), @FindBy(xpath = "//input[@type='checkbox']") })
063    WebElement blockInherit;
064
065    public AccessRightsSubPage(WebDriver driver) {
066        super(driver);
067    }
068
069    public AccessRightsSubPage blockInheritance() {
070        blockInherit.click();
071        return asPage(AccessRightsSubPage.class);
072    }
073
074    public boolean hasPermissionForUser(String permission, String username) {
075        List<WebElement> trElements = driver.findElements(new ByChained(By.className("dataOutput"), By.tagName("tr")));
076        boolean hasPermission = false;
077        for (WebElement trElement : trElements) {
078            List<WebElement> tds = trElement.findElements(By.tagName("td"));
079            if (tds.size() > 3) {
080                String aceUsername = tds.get(1).getText();
081                String aceGrantedPerm = tds.get(2).getText();
082                String aceDeniedPerm = tds.get(3).getText();
083
084                if (username.equals(aceUsername)) {
085                    if (aceGrantedPerm.equals(permission) || "Manage everything".equals(aceGrantedPerm)) {
086                        hasPermission = true;
087                    } else {
088                        if (aceDeniedPerm.equals(permission) || "Manage everything".equals(aceDeniedPerm)) {
089                            hasPermission = false;
090                        }
091                    }
092                }
093
094            }
095
096        }
097
098        // get all the ace
099
100        return hasPermission;
101    }
102
103    /**
104     * @deprecated use {@link #grantPermissionForUser} unless negative ACL are enabled.
105     */
106    @Deprecated
107    public AccessRightsSubPage addPermissionForUser(String username, String permission, boolean grant) {
108
109        boolean allowNegativeACL = hasElement(By.id("add_rights_form:rights_grant_select"));
110
111        if (!allowNegativeACL) {
112            if (grant) {
113                log.warn("addPermissionForUser with negative ACL disabled is deprecated.");
114                return grantPermissionForUser(permission, username);
115            } else {
116                throw new UnsupportedOperationException("Negative ACL are currently disabled!");
117            }
118        }
119
120        WebElement selectGrantElement = driver.findElement(By.id("add_rights_form:rights_grant_select"));
121
122        Select2WidgetElement userSelection = new Select2WidgetElement(
123                driver,
124                driver.findElement(By.xpath("//*[@id='s2id_add_rights_form:nxl_user_group_suggestion:nxw_selection_select2']")),
125                true);
126        userSelection.selectValue(username);
127
128        Select selectGrant = new Select(selectGrantElement);
129
130        if (grant) {
131            selectGrant.selectByValue("Grant");
132
133        } else {
134            selectGrant.selectByValue("Deny");
135        }
136
137        Select selectPermission = new Select(selectPermissionElement);
138        selectPermission.selectByVisibleText(permission);
139
140        addButton.click();
141
142        return asPage(AccessRightsSubPage.class).saveChanges();
143    }
144
145    /**
146     * @since 6.0
147     */
148    public AccessRightsSubPage grantPermissionForUser(String permission, String username) {
149
150        Select2WidgetElement userSelection = new Select2WidgetElement(
151                driver,
152                driver.findElement(By.xpath("//*[@id='s2id_add_rights_form:nxl_user_group_suggestion:nxw_selection_select2']")),
153                true);
154        userSelection.selectValue(username);
155
156        Select selectPermission = new Select(selectPermissionElement);
157        selectPermission.selectByVisibleText(permission);
158
159        addButton.click();
160
161        return asPage(AccessRightsSubPage.class).saveChanges();
162    }
163
164    public AccessRightsSubPage saveChanges() {
165        waitUntilEnabled(validateButton);
166        validateButton.click();
167        return asPage(AccessRightsSubPage.class);
168    }
169
170}