Package org.fao.geonet.repository

Examples of org.fao.geonet.repository.UserGroupRepository


     * @return
     * @throws java.sql.SQLException
     */
    private Element getGroups(ServiceContext context, Profile profile, boolean includingSystemGroups) throws SQLException {
        final GroupRepository groupRepository = context.getBean(GroupRepository.class);
        final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);
        final Sort sort = SortUtils.createSort(Group_.id);

        UserSession session = context.getUserSession();
        if (!session.isAuthenticated()) {
            return groupRepository.findAllAsXml(Specifications.not(GroupSpecs.isReserved()), sort);
        }

        Element result;
        // you're Administrator
        if (Profile.Administrator == session.getProfile()) {
            // return all groups
            result = groupRepository.findAllAsXml(null, sort);
        } else {
            Specifications<UserGroup> spec = Specifications.where(UserGroupSpecs.hasUserId(session.getUserIdAsInt()));
            // you're no Administrator
            // retrieve your groups
      if (profile != null) {
                spec = spec.and(UserGroupSpecs.hasProfile(profile));
            }
            Set<Integer> ids = new HashSet<Integer>(userGroupRepository.findGroupIds(spec));

            // include system groups if requested (used in harvesters)
            if (includingSystemGroups) {
                // these DB keys of system groups are hardcoded !
                for (ReservedGroup reservedGroup : ReservedGroup.values()) {
View Full Code Here


  }

  //--------------------------------------------------------------------------

  private Set<Integer> getUserGroups(ServiceContext context, int userId) throws SQLException {
        final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);

        return new HashSet<Integer>(userGroupRepository.findGroupIds(UserGroupSpecs.hasUserId(userId)));
  }
View Full Code Here

    private void saveUser(LDAPUser userDetails) {
        try {
            UserRepository userRepo = applicationContext.getBean(UserRepository.class);
            GroupRepository groupRepo = applicationContext.getBean(GroupRepository.class);
            UserGroupRepository userGroupRepo = applicationContext.getBean(UserGroupRepository.class);
            LDAPUtils.saveUser(userDetails, userRepo, groupRepo, userGroupRepo, importPrivilegesFromLdap, createNonExistingLdapGroup);
        } catch (Exception e) {
            throw new AuthenticationServiceException(
                    "Unexpected error while saving/updating LDAP user in database",
                    e);
View Full Code Here

        }

        // Remove LDAP user available in db and not in LDAP if not linked to
        // metadata
        final UserRepository userRepository = applicationContext.getBean(UserRepository.class);
        final UserGroupRepository userGroupRepository = applicationContext.getBean(UserGroupRepository.class);
        final Specifications<User> spec = Specifications.where(
                UserSpecs.hasAuthType(LDAPConstants.LDAP_FLAG)
        ).and(
                Specifications.not(UserSpecs.userIsNameNotOneOf(usernames))
        );

        final List<User> usersFound = userRepository.findAll(spec);
        Collection<Integer> userIds = Collections2.transform(usersFound, new Function<User, Integer>() {
            @Nullable
            @Override
            public Integer apply(@Nonnull User input) {
                return input.getId();
            }
        });
        userGroupRepository.deleteAllByIdAttribute(UserGroupId_.userId, userIds);
        userRepository.deleteInBatch(usersFound);
    }
View Full Code Here

                .setAuthType(SHIBBOLETH_FLAG);

        userRepository.save(user);

        if (groupProvided) {
            UserGroupRepository userGroupRepo = context.getBean(UserGroupRepository.class);

            long count = userGroupRepo.count(where(hasGroupId(groupId)).and(hasUserId(userId)));

            if (count == 0) {
                UserGroup userGroup = new UserGroup();
                userGroup.getId().setGroupId(groupId).setUserId(userId);
                userGroupRepo.save(userGroup);
            }
        }

        return user;
    }
View Full Code Here

    if (Profile.Administrator == session.getProfile()) {
      return context.getBean(GroupRepository.class).findAllAsXml(not(GroupSpecs.isReserved()));
    } else {

            final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);
            int userId = session.getUserIdAsInt();
            Specifications<UserGroup> spec = where(UserGroupSpecs.isReservedGroup(false)).and(UserGroupSpecs.hasUserId(userId));

      if (profile != null) {
                spec = spec.and(UserGroupSpecs.hasProfile(Profile.findProfileIgnoreCase(profile)));
            }

            List<Integer> ids = userGroupRepository.findGroupIds(spec);
      Element groups = context.getBean(GroupRepository.class).findAllAsXml(SortUtils.createSort(Group_.id));

      return Lib.element.pruneChildren(groups, new HashSet<Integer>(ids));
    }
  }
View Full Code Here

  {
    String id = Util.getParam(params, Params.ID);


        OperationAllowedRepository operationAllowedRepo = context.getBean(OperationAllowedRepository.class);
        UserGroupRepository userGroupRepo = context.getBean(UserGroupRepository.class);
        GroupRepository groupRepo = context.getBean(GroupRepository.class);

    Integer iId = Integer.valueOf(id);
        List<Integer> reindex = operationAllowedRepo.findAllIds(OperationAllowedSpecs.hasGroupId(iId), OperationAllowedId_.metadataId);

        operationAllowedRepo.deleteAllByIdAttribute(OperationAllowedId_.groupId, iId);
        userGroupRepo.deleteAllByIdAttribute(UserGroupId_.groupId, Arrays.asList(iId));
        groupRepo.delete(iId);
    //--- reindex affected metadata

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    DataManager   dm = gc.getBean(DataManager.class);
View Full Code Here

            //--- retrieve user groups

            Element elGroups = new Element(Geonet.Elem.GROUPS);

            final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);
            final List<UserGroup> userGroups = userGroupRepository.findAll(hasUserId(Integer
                    .valueOf(id)));

            for (UserGroup grp : userGroups) {
                String grpId = "" + grp.getId().getGroupId();

                elGroups.addContent(new Element(Geonet.Elem.ID).setText(grpId).setAttribute("profile", grp.getProfile().name()));
            }

            if (!(myUserId.equals(id)) && myProfile == Profile.UserAdmin) {

                //--- retrieve session user groups and check to see whether this user is
                //--- allowed to get this info
                List<Integer> adminlist = userGroupRepository.findGroupIds(where(hasUserId(Integer.valueOf(myUserId))).or(hasUserId
                        (Integer.valueOf(id))));
                if (adminlist.isEmpty()) {
                    throw new IllegalArgumentException("You don't have rights to do this because the user you want to edit is not part of your group");
                }
            }
View Full Code Here

        UserSession usrSess = context.getUserSession();
        Profile myProfile = usrSess.getProfile();
        String      myUserId  = usrSess.getUserId();

        final UserGroupRepository groupRepository = context.getBean(UserGroupRepository.class);
        final UserRepository userRepository = context.getBean(UserRepository.class);
        @SuppressWarnings("unchecked")
        java.util.List<Element> userGroups = params.getChildren(Params.GROUPS);

        if (profile == Profile.Administrator) {
            userGroups = new ArrayList<Element>();
        }

        if (myProfile == Profile.Administrator ||
        myProfile == Profile.UserAdmin ||
        myUserId.equals(id)) {
            checkAccessRights(operation, id, username, myProfile, myUserId, userGroups, groupRepository);


            User user = getUser(userRepository, operation, id, username);
            if (username != null) {
                user.setUsername(username);
            }

            if (name != null) {
                user.setName(name);
            }
            if (surname != null) {
                user.setSurname(surname);
            }

            if (profile != null) {
                if (!myProfile.getAll().contains(profile)) {
                    throw new IllegalArgumentException("Trying to set profile to "+profile+" max profile permitted is: "+myProfile);
                }
                user.setProfile(profile);
            }
            if (kind != null) {
                user.setKind(kind);
            }
            if (organ != null) {
                user.setOrganisation(organ);
            }

            Address addressEntity;
            boolean hasNoAddress = user.getAddresses().isEmpty();
            if (hasNoAddress) {
                addressEntity = new Address();
            } else {
                addressEntity = user.getAddresses().iterator().next();

            }
            if (address != null) {
                addressEntity.setAddress(address);
            }
            if (city != null) {
                addressEntity.setCity(city);
            }
            if (state != null) {
                addressEntity.setState(state);
            }
            if (zip != null) {
                addressEntity.setZip(zip);
            }
            if (country != null) {
                addressEntity.setCountry(country);
            }

            if (hasNoAddress) {
                user.getAddresses().add(addressEntity);
            }

            if (email != null) {
                user.getEmailAddresses().add(email);
            }


            if (password != null) {
                user.getSecurity().setPassword(PasswordUtil.encode(context, password));
            } else if (operation.equals(Params.Operation.RESETPW)) {
                throw new IllegalArgumentException("password is a required parameter for operation: " + Params.Operation.RESETPW);
            }

            // -- For adding new user
            if (operation.equals(Params.Operation.NEWUSER)) {
                user = userRepository.save(user);

        setUserGroups(user, params, context);
      } else if (operation.equals(Params.Operation.FULLUPDATE) || operation.equals(Params.Operation.EDITINFO)) {
                user = userRepository.save(user);

                //--- add groups
                groupRepository.deleteAllByIdAttribute(UserGroupId_.userId, Arrays.asList(user.getId()));

                setUserGroups(user, params, context);
      } else if (operation.equals(Params.Operation.RESETPW)) {
             user = userRepository.save(user);
      } else {
View Full Code Here

    private void setUserGroups(final User user, final Element params, final ServiceContext context) throws Exception {
    String[] profiles = {Profile.UserAdmin.name(), Profile.Reviewer.name(), Profile.Editor.name(), Profile.RegisteredUser.name()};
        Collection<UserGroup> toAdd = new ArrayList<UserGroup>();
        Set<String> listOfAddedProfiles = new HashSet<String>();
        final GroupRepository groupRepository = context.getBean(GroupRepository.class);
        final UserGroupRepository userGroupRepository = context.getBean(UserGroupRepository.class);

        for (String profile : profiles) {
       
      @SuppressWarnings("unchecked")
            java.util.List<Element> userGroups = params.getChildren(Params.GROUPS + '_' + profile);
      for (Element element : userGroups) {
        String groupEl = element.getText();
        if (!groupEl.equals("")) {
          int groupId = Integer.valueOf(groupEl);
                    Group group = groupRepository.findOne(groupId);

                    // Combine all groups editor and reviewer groups
                    if (profile.equals(Profile.Reviewer.name())) {
                        final UserGroup userGroup = new UserGroup()
                                .setGroup(group)
                                .setProfile(Profile.Editor)
                                .setUser(user);
                        String key = Profile.Editor.toString() + group.getId();
                        if (!listOfAddedProfiles.contains(key)) {
                            toAdd.add(userGroup);
                            listOfAddedProfiles.add(key);
                        }
          }

                    final UserGroup userGroup = new UserGroup()
                            .setGroup(group)
                            .setProfile(Profile.findProfileIgnoreCase(profile))
                            .setUser(user);
                    String key = profile + group.getId();
                    if (!listOfAddedProfiles.contains(key)) {
                        toAdd.add(userGroup);
                        listOfAddedProfiles.add(key);
                    }
        }
      }
    }

        userGroupRepository.save(toAdd);

  }
View Full Code Here

TOP

Related Classes of org.fao.geonet.repository.UserGroupRepository

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.