private User createDeviceUser(
      User creatorUser, String deviceConfigName, ServiceContext serviceContext)
      throws SystemException, PortalException {

    debug("Getting device role");
    Role deviceRole =
        RoleLocalServiceUtil.getRole(
            creatorUser.getCompanyId(), PrimumConstantsLocalServiceUtil.getPrimumRole_DEVICE());

    if (creatorUser.getOrganizations().size() != 1) {
      throw new IllegalArgumentException(
          "Usert must belong to EXACTLY ONE organization but "
              + creatorUser.getOrganizations().size()
              + " were found!");
    }
    Organization organization = creatorUser.getOrganizations().get(0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    debug("creating deviceUser");
    boolean autoPassword = true;
    String password1 = StringPool.BLANK;
    String password2 = StringPool.BLANK;
    boolean autoScreenName = false;
    String screenName = getUserScreenName(creatorUser.getOrganizations());
    String emailAddress = screenName + "@primum.es";
    long facebookId = 0;
    String openId = StringPool.BLANK;
    Locale locale =
        (serviceContext.getLocale() != null) ? serviceContext.getLocale() : Locale.getDefault();
    String firstName = deviceConfigName;
    String middleName = StringPool.BLANK;
    String lastName = StringPool.BLANK;
    int prefixId = 0;
    int suffixId = 0;
    boolean male = false;
    int birthdayMonth = calendar.get(Calendar.MONTH);
    int birthdayDay = calendar.get(Calendar.DAY_OF_MONTH);
    int birthdayYear = calendar.get(Calendar.YEAR);
    String jobTitle = StringPool.BLANK;
    long[] groupIds = new long[] {organization.getGroupId()};
    long[] organizationIds = creatorUser.getOrganizationIds();
    long[] roleIds = new long[] {deviceRole.getRoleId()};
    long[] userGroupIds = null;
    boolean sendEmail = true;
    User newUser =
        UserLocalServiceUtil.addUser(
            creatorUser.getUserId(),
            creatorUser.getCompanyId(),
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            facebookId,
            openId,
            locale,
            firstName,
            middleName,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            groupIds,
            organizationIds,
            roleIds,
            userGroupIds,
            sendEmail,
            serviceContext);

    debug("deviceUser created: " + newUser);
    //		createContact4User (newUser);

    return newUser;
  }
Esempio n. 2
0
  public static void getRole(HttpServletRequest request) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

    long roleId = ParamUtil.getLong(request, "roleId");

    Role role = null;

    Group group = (Group) request.getAttribute(WebKeys.GROUP);

    if ((group != null) && group.isOrganization()) {
      long organizationId = group.getOrganizationId();

      while (organizationId != OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {

        Organization organization = OrganizationLocalServiceUtil.getOrganization(organizationId);

        long organizationGroupId = organization.getGroupId();

        if (GroupPermissionUtil.contains(
                permissionChecker, organizationGroupId, ActionKeys.ASSIGN_USER_ROLES)
            || OrganizationPermissionUtil.contains(
                permissionChecker, organizationId, ActionKeys.ASSIGN_USER_ROLES)
            || UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                themeDisplay.getUserId(),
                organizationGroupId,
                RoleConstants.ORGANIZATION_ADMINISTRATOR,
                true)
            || UserGroupRoleLocalServiceUtil.hasUserGroupRole(
                themeDisplay.getUserId(),
                organizationGroupId,
                RoleConstants.ORGANIZATION_OWNER,
                true)) {

          if (roleId > 0) {
            role = RoleLocalServiceUtil.getRole(roleId);
          }

          break;
        }

        organizationId = organization.getParentOrganizationId();
      }

      if ((roleId > 0) && (role == null)) {
        role = RoleServiceUtil.getRole(roleId);
      }
    } else if ((group != null) && group.isRegularSite()) {
      if (GroupPermissionUtil.contains(permissionChecker, group, ActionKeys.ASSIGN_USER_ROLES)
          || UserGroupRoleLocalServiceUtil.hasUserGroupRole(
              themeDisplay.getUserId(), group.getGroupId(), RoleConstants.SITE_ADMINISTRATOR, true)
          || UserGroupRoleLocalServiceUtil.hasUserGroupRole(
              themeDisplay.getUserId(), group.getGroupId(), RoleConstants.SITE_OWNER, true)) {

        if (roleId > 0) {
          role = RoleLocalServiceUtil.getRole(roleId);
        }
      } else {
        if (roleId > 0) {
          role = RoleServiceUtil.getRole(roleId);
        }
      }
    } else {
      if (roleId > 0) {
        role = RoleServiceUtil.getRole(roleId);
      }
    }

    request.setAttribute(WebKeys.ROLE, role);
  }