Exemplo n.º 1
0
  public Group getGroup(Principal principal, Properties properties) throws IOException {
    Group res = null;

    {
      // Set group from translated principal, if possible:
      {
        String name = PrincipalUtil.getNameStripped(principal);
        if (name != null) {
          if (name.equals(ApplicationUserRoles.ROLE_USER)) {
            res = getGroupUser(properties);
          } else {
            if (name.equals(ApplicationUserRoles.ROLE_ADMINISTRATOR)) {
              res = getGroupAdministrator(properties);
            } else {
              // Ignore!
            }
          }
        }
      }

      if (res == null) // if no result has been set...
      {
        res = super.getGroup(principal, properties); // get group by its direct name!
      }
    }

    return res;
  }
Exemplo n.º 2
0
  protected List<AbstractSession> filterByUserPrincipal(
      Collection<AbstractSession> values, Principal userPrincipal) throws IOException {
    List<AbstractSession> res = null;

    {
      if (values != null) {
        res = new ArrayList<AbstractSession>();

        for (AbstractSession session : values) {
          expandSessionPrincipal(session);
        }

        if (userPrincipal == null) {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (p == null) {
              res.add(session);
            }
          }
        } else {
          for (AbstractSession session : values) {
            Principal p = session.getUserPrincipal();
            if (PrincipalUtil.equalsIgnoreRealm(userPrincipal, p)) {
              res.add(session);
            }
          }
        }
      }
    }

    return res;
  }
Exemplo n.º 3
0
  protected List<String> getIncludeUserRolesFromConfig(Principal userPrincipal) throws IOException {
    List<String> res = null;

    {
      if (userPrincipal != null) {
        String userName = PrincipalUtil.getNameStripped(userPrincipal);

        if (userName != null) {
          String property = "security.authorization.user." + userName + ".roles";
          res = getUserRolesFromConfig(property);
        }
      }
    }

    return res;
  }
Exemplo n.º 4
0
  protected String getUserPresentationNameFromConfig(Principal userPrincipal) throws IOException {
    String res = null;

    {
      if (userPrincipal != null) {
        String userName = PrincipalUtil.getNameStripped(userPrincipal);

        if (userName != null) {
          String property = "security.authorization.user." + userName + ".presentation-name";

          ConfigFactory f = DefaultConfigFactory.getInstance();
          Config c = f.getConfig();

          res = c.getProperty(property);
        }
      }
    }

    return res;
  }
Exemplo n.º 5
0
  public User getUser(Principal principal, Properties properties) throws IOException {
    User res = null;

    {
      AbstractUserAuthorizor.DefaultUser user = null; // user created by this

      // Set 'user':
      {
        User u = super.getUser(principal, properties); // get user returned from nested resource

        if (u == null) {
          user = new AbstractUserAuthorizor.DefaultUser();

          // Set principal:
          {
            String userName = PrincipalUtil.getNameStripped(principal);
            Principal userPrincipal = new SimplePrincipal(userName);
            user.setPrincipal(userPrincipal);
          }
        } else {
          user = new AbstractUserAuthorizor.DefaultUser(u);
        }
      }

      // Override group principals:
      {
        List<Principal> l = expandGroupPrincipals(user); // new list of group-principals
        user.setGroupPrincipals(l); // overwrite the original group principals
      }

      // Override user roles:
      {
        List<String> userRoles = new ArrayList<String>();

        // Add basic user roles:
        {
          List<String> l = user.getUserRoles();
          if (l != null) {
            userRoles.addAll(l);
          }
        }

        // Add special, additional user roles:
        {
          List<String> l = getIncludeUserRolesFromConfig(principal);
          if (l != null) {
            userRoles.addAll(l); // add!
          }
        }

        // Expand all user roles:
        {
          userRoles =
              ApplicationUserRoles.expandUserRoles(
                  userRoles); // expanded user-roles, sorted, duplicates removed!
        }

        // Remove special, excluded user roles:
        {
          List<String> l = getExcludeUserRolesFromConfig(principal);
          if (l != null) {
            l =
                ApplicationUserRoles.expandUserRoles(
                    l); // expanded user-roles, sorted, duplicates removed!
            userRoles.removeAll(l); // remove!
          }
        }

        user.setUserRoles(userRoles); // overwrite the original user roles
      }

      // Override full name:
      {
        Principal userPrincipal = user.getPrincipal();
        String fullName = getUserPresentationNameFromConfig(userPrincipal);
        if (fullName != null) {
          fullName = fullName.trim();

          if (fullName.length() > 0) {
            user.setFullName(fullName);
          }
        }
      }

      res = user;
    }

    return res;
  }