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 */
038public class AccessRightsSubPage extends AbstractPage {
039
040    private static final Log log = LogFactory.getLog(AccessRightsSubPage.class);
041
042    /*
043     * @Required
044     * @FindBy(id = "add_rights_form:nxl_user_group_suggestion:nxw_selection_suggest") WebElement
045     * userSelectionSuggestInputText;
046     */
047
048    @Required
049    @FindBy(id = "add_rights_form:rights_permission_select")
050    WebElement selectPermissionElement;
051
052    @Required
053    @FindBy(id = "add_rights_form:rights_add_button")
054    WebElement addButton;
055
056    @FindBy(id = "validate_rights:document_rights_validate_button")
057    WebElement validateButton;
058
059    @Required
060    @FindBys({ @FindBy(id = "block_inherit"), @FindBy(xpath = "//input[@type='checkbox']") })
061    WebElement blockInherit;
062
063    public AccessRightsSubPage(WebDriver driver) {
064        super(driver);
065    }
066
067    public AccessRightsSubPage blockInheritance() {
068        blockInherit.click();
069        return asPage(AccessRightsSubPage.class);
070    }
071
072    public boolean hasPermissionForUser(String permission, String username) {
073        List<WebElement> trElements = driver.findElements(new ByChained(By.className("dataOutput"), By.tagName("tr")));
074        boolean hasPermission = false;
075        for (WebElement trElement : trElements) {
076            List<WebElement> tds = trElement.findElements(By.tagName("td"));
077            if (tds.size() > 3) {
078                String aceUsername = tds.get(1).getText();
079                String aceGrantedPerm = tds.get(2).getText();
080                String aceDeniedPerm = tds.get(3).getText();
081
082                if (username.equals(aceUsername)) {
083                    if (aceGrantedPerm.equals(permission) || "Manage everything".equals(aceGrantedPerm)) {
084                        hasPermission = true;
085                    } else {
086                        if (aceDeniedPerm.equals(permission) || "Manage everything".equals(aceDeniedPerm)) {
087                            hasPermission = false;
088                        }
089                    }
090                }
091
092            }
093
094        }
095
096        // get all the ace
097
098        return hasPermission;
099    }
100
101    /**
102     * @deprecated use {@link #grantPermissionForUser} unless negative ACL are enabled.
103     */
104    @Deprecated
105    public AccessRightsSubPage addPermissionForUser(String username, String permission, boolean grant) {
106
107        boolean allowNegativeACL = hasElement(By.id("add_rights_form:rights_grant_select"));
108
109        if (!allowNegativeACL) {
110            if (grant) {
111                log.warn("addPermissionForUser with negative ACL disabled is deprecated.");
112                return grantPermissionForUser(permission, username);
113            } else {
114                throw new UnsupportedOperationException("Negative ACL are currently disabled!");
115            }
116        }
117
118        WebElement selectGrantElement = driver.findElement(By.id("add_rights_form:rights_grant_select"));
119
120        Select2WidgetElement userSelection = new Select2WidgetElement(
121                driver,
122                driver.findElement(By.xpath("//*[@id='s2id_add_rights_form:nxl_user_group_suggestion:nxw_selection_select2']")),
123                true);
124        userSelection.selectValue(username);
125
126        Select selectGrant = new Select(selectGrantElement);
127
128        if (grant) {
129            selectGrant.selectByValue("Grant");
130
131        } else {
132            selectGrant.selectByValue("Deny");
133        }
134
135        Select selectPermission = new Select(selectPermissionElement);
136        selectPermission.selectByVisibleText(permission);
137
138        addButton.click();
139
140        return asPage(AccessRightsSubPage.class).saveChanges();
141    }
142
143    /**
144     * @since 6.0
145     */
146    public AccessRightsSubPage grantPermissionForUser(String permission, String username) {
147
148        Select2WidgetElement userSelection = new Select2WidgetElement(
149                driver,
150                driver.findElement(By.xpath("//*[@id='s2id_add_rights_form:nxl_user_group_suggestion:nxw_selection_select2']")),
151                true);
152        userSelection.selectValue(username);
153
154        Select selectPermission = new Select(selectPermissionElement);
155        selectPermission.selectByVisibleText(permission);
156
157        addButton.click();
158
159        return asPage(AccessRightsSubPage.class).saveChanges();
160    }
161
162    public AccessRightsSubPage saveChanges() {
163        waitUntilEnabled(validateButton);
164        validateButton.click();
165        return asPage(AccessRightsSubPage.class);
166    }
167
168}