Search in sources :

Example 1 with PersonInfo

use of org.alfresco.service.cmr.security.PersonService.PersonInfo in project acs-community-packaging by Alfresco.

the class BaseAssociationEditor method getAvailableOptions.

/**
 * Retrieves the available options for the current association
 *
 * @param context Faces Context
 * @param contains The contains part of the query
 */
protected void getAvailableOptions(FacesContext context, String contains) {
    AssociationDefinition assocDef = getAssociationDefinition(context);
    if (assocDef != null) {
        // find and show all the available options for the current association
        String type = assocDef.getTargetClass().getName().toString();
        if (type.equals(ContentModel.TYPE_AUTHORITY_CONTAINER.toString())) {
            UserTransaction tx = null;
            try {
                tx = Repository.getUserTransaction(context, true);
                tx.begin();
                String safeContains = null;
                if (contains != null && contains.length() > 0) {
                    safeContains = Utils.remove(contains.trim(), "\"");
                    safeContains = safeContains.toLowerCase();
                }
                // get all available groups
                AuthorityService authorityService = Repository.getServiceRegistry(context).getAuthorityService();
                Set<String> groups = authorityService.getAllAuthoritiesInZone(AuthorityService.ZONE_APP_DEFAULT, AuthorityType.GROUP);
                this.availableOptions = new ArrayList<NodeRef>(groups.size());
                // get the NodeRef for each matching group
                AuthorityDAO authorityDAO = (AuthorityDAO) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("authorityDAO");
                if (authorityDAO != null) {
                    List<String> matchingGroups = new ArrayList<String>();
                    String groupDisplayName;
                    for (String group : groups) {
                        // get display name, if not present strip prefix from group id
                        groupDisplayName = authorityService.getAuthorityDisplayName(group);
                        if (groupDisplayName == null || groupDisplayName.length() == 0) {
                            groupDisplayName = group.substring(PermissionService.GROUP_PREFIX.length());
                        }
                        // otherwise just add the group name to the sorted set
                        if (safeContains != null) {
                            if (groupDisplayName.toLowerCase().indexOf(safeContains) != -1) {
                                matchingGroups.add(group);
                            }
                        } else {
                            matchingGroups.add(group);
                        }
                    }
                    // sort the group names
                    Collections.sort(matchingGroups, new SimpleStringComparator());
                    // go through the sorted set and get the NodeRef for each group
                    for (String groupName : matchingGroups) {
                        NodeRef groupRef = authorityDAO.getAuthorityNodeRefOrNull(groupName);
                        if (groupRef != null) {
                            this.availableOptions.add(groupRef);
                        }
                    }
                }
                // commit the transaction
                tx.commit();
            } catch (Throwable err) {
                Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
                this.availableOptions = Collections.<NodeRef>emptyList();
                try {
                    if (tx != null) {
                        tx.rollback();
                    }
                } catch (Exception tex) {
                }
            }
        } else if (type.equals(ContentModel.TYPE_PERSON.toString())) {
            List<Pair<QName, String>> filter = (contains != null && contains.trim().length() > 0) ? Utils.generatePersonFilter(contains.trim()) : null;
            // Always sort by last name, then first name
            List<Pair<QName, Boolean>> sort = new ArrayList<Pair<QName, Boolean>>();
            sort.add(new Pair<QName, Boolean>(ContentModel.PROP_LASTNAME, true));
            sort.add(new Pair<QName, Boolean>(ContentModel.PROP_FIRSTNAME, true));
            // Log the filtering
            if (logger.isDebugEnabled())
                logger.debug("Query filter: " + filter);
            // How many to limit too?
            int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
            if (maxResults <= 0) {
                maxResults = Utils.getPersonMaxResults();
            }
            List<PersonInfo> persons = Repository.getServiceRegistry(context).getPersonService().getPeople(filter, true, sort, new PagingRequest(maxResults, null)).getPage();
            // Save the results
            List<NodeRef> nodes = new ArrayList<NodeRef>(persons.size());
            for (PersonInfo person : persons) {
                nodes.add(person.getNodeRef());
            }
            this.availableOptions = nodes;
        } else {
            // for all other types/aspects perform a lucene search
            StringBuilder query = new StringBuilder("+TYPE:\"");
            if (assocDef.getTargetClass().isAspect()) {
                query = new StringBuilder("+ASPECT:\"");
            } else {
                query = new StringBuilder("+TYPE:\"");
            }
            query.append(type);
            query.append("\"");
            if (contains != null && contains.trim().length() != 0) {
                String safeContains = null;
                if (contains != null && contains.length() > 0) {
                    safeContains = Utils.remove(contains.trim(), "\"");
                    safeContains = safeContains.toLowerCase();
                }
                query.append(" AND +@");
                String nameAttr = Repository.escapeQName(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "name"));
                query.append(nameAttr);
                query.append(":\"*" + safeContains + "*\"");
            }
            int maxResults = Application.getClientConfig(context).getSelectorsSearchMaxResults();
            if (logger.isDebugEnabled()) {
                logger.debug("Query: " + query.toString());
                logger.debug("Max results size: " + maxResults);
            }
            SearchParameters searchParams = new SearchParameters();
            searchParams.addStore(Repository.getStoreRef());
            searchParams.setLanguage(SearchService.LANGUAGE_LUCENE);
            searchParams.setQuery(query.toString());
            if (maxResults > 0) {
                searchParams.setLimit(maxResults);
                searchParams.setLimitBy(LimitBy.FINAL_SIZE);
            }
            ResultSet results = null;
            try {
                results = Repository.getServiceRegistry(context).getSearchService().query(searchParams);
                this.availableOptions = results.getNodeRefs();
            } catch (SearcherException se) {
                logger.info("Search failed for: " + query, se);
                Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_QUERY));
            } finally {
                if (results != null) {
                    results.close();
                }
            }
        }
        if (logger.isDebugEnabled())
            logger.debug("Found " + this.availableOptions.size() + " available options");
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) QName(org.alfresco.service.namespace.QName) AuthorityService(org.alfresco.service.cmr.security.AuthorityService) ArrayList(java.util.ArrayList) AuthorityDAO(org.alfresco.repo.security.authority.AuthorityDAO) SearcherException(org.alfresco.repo.search.SearcherException) AbortProcessingException(javax.faces.event.AbortProcessingException) IOException(java.io.IOException) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) SearcherException(org.alfresco.repo.search.SearcherException) ResultSet(org.alfresco.service.cmr.search.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 2 with PersonInfo

use of org.alfresco.service.cmr.security.PersonService.PersonInfo in project acs-community-packaging by Alfresco.

the class UsersDialog method search.

/**
 * Event handler called when the user wishes to search for a user
 *
 * @return The outcome
 */
public String search() {
    properties.getUsersRichList().setValue(null);
    if (properties.getSearchCriteria() == null || properties.getSearchCriteria().trim().length() == 0) {
        this.users = Collections.<Node>emptyList();
    } else {
        FacesContext context = FacesContext.getCurrentInstance();
        UserTransaction tx = null;
        try {
            tx = Repository.getUserTransaction(context, true);
            tx.begin();
            // define the query to find people by their first or last name
            String search = properties.getSearchCriteria();
            if (logger.isDebugEnabled()) {
                logger.debug("Query filter: " + search);
            }
            List<PersonInfo> persons = properties.getPersonService().getPeople(Utils.generatePersonFilter(search), true, Utils.generatePersonSort(), new PagingRequest(Utils.getPersonMaxResults(), null)).getPage();
            if (logger.isDebugEnabled()) {
                logger.debug("Found " + persons.size() + " users");
            }
            this.users = new ArrayList<Node>(persons.size());
            for (PersonInfo person : persons) {
                // create our Node representation
                MapNode node = new MapNode(person.getNodeRef());
                // set data binding properties
                // this will also force initialisation of the props now during the UserTransaction
                // it is much better for performance to do this now rather than during page bind
                Map<String, Object> props = node.getProperties();
                String firstName = (String) props.get("firstName");
                String lastName = (String) props.get("lastName");
                props.put("fullName", (firstName != null ? firstName : "") + ' ' + (lastName != null ? lastName : ""));
                NodeRef homeFolderNodeRef = (NodeRef) props.get("homeFolder");
                if (homeFolderNodeRef != null) {
                    props.put("homeSpace", homeFolderNodeRef);
                }
                node.addPropertyResolver("sizeLatest", this.resolverUserSizeLatest);
                node.addPropertyResolver("quota", this.resolverUserQuota);
                node.addPropertyResolver("isMutable", this.resolverUserMutable);
                this.users.add(node);
            }
            // commit the transaction
            tx.commit();
        } catch (InvalidNodeRefException refErr) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_NODEREF), new Object[] { "root" }));
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        } catch (Exception err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        }
    }
    // return null to stay on the same page
    return null;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) Node(org.alfresco.web.bean.repository.Node) MapNode(org.alfresco.web.bean.repository.MapNode) MapNode(org.alfresco.web.bean.repository.MapNode) PagingRequest(org.alfresco.query.PagingRequest) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) ReportedException(org.alfresco.web.ui.common.ReportedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 3 with PersonInfo

use of org.alfresco.service.cmr.security.PersonService.PersonInfo in project acs-community-packaging by Alfresco.

the class BaseInviteUsersWizard method pickerCallback.

/**
 * Query callback method executed by the Generic Picker component.
 * This method is part of the contract to the Generic Picker, it is up to the backing bean
 * to execute whatever query is appropriate and return the results.
 *
 * @param filterIndex        Index of the filter drop-down selection
 * @param contains           Text from the contains textbox
 *
 * @return An array of SelectItem objects containing the results to display in the picker.
 */
public SelectItem[] pickerCallback(int filterIndex, String contains) {
    FacesContext context = FacesContext.getCurrentInstance();
    // quick exit if not enough characters entered for a search
    String search = contains.trim();
    int searchMin = Application.getClientConfig(context).getPickerSearchMinimum();
    if (search.length() < searchMin) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, MSG_SEARCH_MINIMUM), searchMin));
        return new SelectItem[0];
    }
    SelectItem[] items;
    this.maxUsersReturned = false;
    UserTransaction tx = null;
    try {
        tx = Repository.getUserTransaction(context, true);
        tx.begin();
        int maxResults = Application.getClientConfig(context).getInviteUsersMaxResults();
        if (maxResults <= 0) {
            maxResults = Utils.getPersonMaxResults();
        }
        List<SelectItem> results;
        if (filterIndex == 0) {
            // Use lucene search to retrieve user details
            List<Pair<QName, String>> filter = null;
            if (search == null || search.length() == 0) {
            // if there is no search term, search for all people
            } else {
                filter = Utils.generatePersonFilter(search);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Maximum invite users results size: " + maxResults);
                logger.debug("Using query filter to find users: " + filter);
            }
            List<PersonInfo> persons = getPersonService().getPeople(filter, true, Utils.generatePersonSort(), new PagingRequest(maxResults, null)).getPage();
            results = new ArrayList<SelectItem>(persons.size());
            for (int index = 0; index < persons.size(); index++) {
                PersonInfo person = persons.get(index);
                String firstName = person.getFirstName();
                String lastName = person.getLastName();
                String username = person.getUserName();
                if (username != null) {
                    String name = (firstName != null ? firstName : "") + ' ' + (lastName != null ? lastName : "");
                    SelectItem item = new SortableSelectItem(username, name + " [" + username + "]", lastName != null ? lastName : username);
                    results.add(item);
                }
            }
        } else {
            results = addGroupItems(search, maxResults);
        }
        items = new SelectItem[results.size()];
        results.toArray(items);
        Arrays.sort(items);
        // set the maximum users returned flag if appropriate
        if (results.size() == maxResults) {
            this.maxUsersReturned = true;
        }
        // commit the transaction
        tx.commit();
    } catch (BooleanQuery.TooManyClauses clauses) {
        Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), "too_many_users"));
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        items = new SelectItem[0];
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        items = new SelectItem[0];
    }
    return items;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) BooleanQuery(org.apache.lucene.search.BooleanQuery) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) PagingRequest(org.alfresco.query.PagingRequest) IOException(java.io.IOException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) SelectItem(javax.faces.model.SelectItem) Pair(org.alfresco.util.Pair)

Example 4 with PersonInfo

use of org.alfresco.service.cmr.security.PersonService.PersonInfo in project acs-community-packaging by Alfresco.

the class BaseReassignDialog method pickerCallback.

/**
 * Query callback method executed by the Generic Picker component.
 * This method is part of the contract to the Generic Picker, it is up to the backing bean
 * to execute whatever query is appropriate and return the results.
 *
 * @param filterIndex        Index of the filter drop-down selection
 * @param contains           Text from the contains textbox
 *
 * @return An array of SelectItem objects containing the results to display in the picker.
 */
public SelectItem[] pickerCallback(int filterIndex, String contains) {
    FacesContext context = FacesContext.getCurrentInstance();
    // quick exit if not enough characters entered for a search
    String search = contains.trim();
    int searchMin = Application.getClientConfig(context).getPickerSearchMinimum();
    if (search.length() < searchMin) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, MSG_SEARCH_MINIMUM), searchMin));
        return new SelectItem[0];
    }
    SelectItem[] items;
    UserTransaction tx = null;
    ResultSet resultSet = null;
    try {
        tx = Repository.getUserTransaction(context, true);
        tx.begin();
        int maxResults = Application.getClientConfig(context).getInviteUsersMaxResults();
        if (maxResults <= 0) {
            maxResults = Utils.getPersonMaxResults();
        }
        List<PersonInfo> persons = getPersonService().getPeople(Utils.generatePersonFilter(contains.trim()), true, Utils.generatePersonSort(), new PagingRequest(maxResults, null)).getPage();
        ArrayList<SelectItem> itemList = new ArrayList<SelectItem>(persons.size());
        for (PersonInfo person : persons) {
            String username = person.getUserName();
            if (AuthenticationUtil.getGuestUserName().equals(username) == false) {
                String firstName = person.getFirstName();
                String lastName = person.getLastName();
                String name = (firstName != null ? firstName : "") + ' ' + (lastName != null ? lastName : "");
                SelectItem item = new SortableSelectItem(username, name + " [" + username + "]", lastName != null ? lastName : username);
                itemList.add(item);
            }
        }
        items = new SelectItem[itemList.size()];
        itemList.toArray(items);
        Arrays.sort(items);
        // commit the transaction
        tx.commit();
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
        items = new SelectItem[0];
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
    }
    return items;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) ArrayList(java.util.ArrayList) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) PagingRequest(org.alfresco.query.PagingRequest) SortableSelectItem(org.alfresco.web.ui.common.SortableSelectItem) SelectItem(javax.faces.model.SelectItem) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Example 5 with PersonInfo

use of org.alfresco.service.cmr.security.PersonService.PersonInfo in project acs-community-packaging by Alfresco.

the class DeleteUserDialog method search.

public String search() {
    if (this.searchCriteria == null || this.searchCriteria.length() == 0) {
        this.users = Collections.<Node>emptyList();
    } else {
        FacesContext context = FacesContext.getCurrentInstance();
        UserTransaction tx = null;
        try {
            tx = Repository.getUserTransaction(context, true);
            tx.begin();
            // define the query to find people by their first or last name
            String search = ISO9075.encode(this.searchCriteria);
            List<Pair<QName, String>> filter = Utils.generatePersonFilter(search);
            if (logger.isDebugEnabled()) {
                logger.debug("Query filter: " + filter);
            }
            List<PersonInfo> persons = getPersonService().getPeople(filter, true, Utils.generatePersonSort(), new PagingRequest(Utils.getPersonMaxResults(), null)).getPage();
            if (logger.isDebugEnabled()) {
                logger.debug("Found " + persons.size() + " users");
            }
            this.users = new ArrayList<Node>(persons.size());
            for (PersonInfo person : persons) {
                // create our Node representation
                MapNode node = new MapNode(person.getNodeRef());
                // set data binding properties
                // this will also force initialisation of the props now during the UserTransaction
                // it is much better for performance to do this now rather than during page bind
                Map<String, Object> props = node.getProperties();
                props.put("fullName", ((String) props.get("firstName")) + ' ' + ((String) props.get("lastName")));
                NodeRef homeFolderNodeRef = (NodeRef) props.get("homeFolder");
                if (homeFolderNodeRef != null) {
                    props.put("homeSpace", homeFolderNodeRef);
                }
                this.users.add(node);
            }
            // commit the transaction
            tx.commit();
        } catch (InvalidNodeRefException refErr) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_NODEREF), new Object[] { "root" }));
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        } catch (Exception err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(context, Repository.ERROR_GENERIC), err.getMessage()), err);
            this.users = Collections.<Node>emptyList();
            try {
                if (tx != null) {
                    tx.rollback();
                }
            } catch (Exception tex) {
            }
        }
    }
    // return null to stay on the same page
    return null;
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) PersonInfo(org.alfresco.service.cmr.security.PersonService.PersonInfo) Node(org.alfresco.web.bean.repository.Node) MapNode(org.alfresco.web.bean.repository.MapNode) MapNode(org.alfresco.web.bean.repository.MapNode) PagingRequest(org.alfresco.query.PagingRequest) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) ReportedException(org.alfresco.web.ui.common.ReportedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) Pair(org.alfresco.util.Pair)

Aggregations

UserTransaction (javax.transaction.UserTransaction)6 PagingRequest (org.alfresco.query.PagingRequest)6 PersonInfo (org.alfresco.service.cmr.security.PersonService.PersonInfo)6 FacesContext (javax.faces.context.FacesContext)5 ArrayList (java.util.ArrayList)3 SelectItem (javax.faces.model.SelectItem)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Pair (org.alfresco.util.Pair)3 SortableSelectItem (org.alfresco.web.ui.common.SortableSelectItem)3 IOException (java.io.IOException)2 List (java.util.List)2 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)2 ResultSet (org.alfresco.service.cmr.search.ResultSet)2 MapNode (org.alfresco.web.bean.repository.MapNode)2 Node (org.alfresco.web.bean.repository.Node)2 ReportedException (org.alfresco.web.ui.common.ReportedException)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 AbortProcessingException (javax.faces.event.AbortProcessingException)1 SearcherException (org.alfresco.repo.search.SearcherException)1 AuthorityDAO (org.alfresco.repo.security.authority.AuthorityDAO)1