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