Example #1
0
    @Override
    public void execute(Event<UIAvatarUploader> event) throws Exception {
      WebuiRequestContext ctx = event.getRequestContext();
      UIApplication uiApplication = ctx.getUIApplication();
      UIAvatarUploader uiAvatarUploader = event.getSource();
      UIFormUploadInput uiAvatarUploadInput = uiAvatarUploader.getChild(UIFormUploadInput.class);
      UIPopupWindow uiPopup = uiAvatarUploader.getParent();
      InputStream uploadedStream = uiAvatarUploadInput.getUploadDataAsStream();

      if (uploadedStream == null) {
        uiApplication.addMessage(
            new ApplicationMessage(MSG_IMG_NOT_UPLOADED, null, ApplicationMessage.ERROR));
        ctx.addUIComponentToUpdateByAjax(uiAvatarUploader);
        return;
      }
      UploadResource uploadResource = uiAvatarUploadInput.getUploadResource();

      String mimeType = uploadResource.getMimeType();
      String uploadId = uiAvatarUploadInput.getUploadId();
      if (!uiAvatarUploader.isAcceptedMimeType(mimeType)) {
        UploadService uploadService =
            (UploadService) PortalContainer.getComponent(UploadService.class);
        uploadService.removeUploadResource(uploadId);
        uiApplication.addMessage(
            new ApplicationMessage(MSG_MIMETYPE_NOT_ACCEPTED, null, ApplicationMessage.ERROR));
        ctx.addUIComponentToUpdateByAjax(uiAvatarUploader);
      } else {
        MimeTypeResolver mimeTypeResolver = new MimeTypeResolver();
        String fileName = uploadResource.getFileName();

        // @since 1.1.3
        String extension = mimeTypeResolver.getExtension(mimeType);
        if ("".equals(extension)) {
          mimeType = uiAvatarUploader.getStandardMimeType(mimeType);
        }

        // Resize avatar to fixed width if can't(avatarAttachment == null) keep
        // origin avatar
        AvatarAttachment avatarAttachment =
            ImageUtils.createResizedAvatarAttachment(
                uploadedStream, WIDTH, 0, null, fileName, mimeType, null);
        if (avatarAttachment == null) {
          avatarAttachment =
              new AvatarAttachment(
                  null, fileName, mimeType, uploadedStream, null, System.currentTimeMillis());
        }

        UploadService uploadService =
            (UploadService) PortalContainer.getComponent(UploadService.class);
        uploadService.removeUploadResource(uploadId);
        UIAvatarUploadContent uiAvatarUploadContent =
            uiAvatarUploader.createUIComponent(UIAvatarUploadContent.class, null, null);
        uiAvatarUploadContent.setAvatarAttachment(avatarAttachment);
        uiPopup.setUIComponent(uiAvatarUploadContent);
        ctx.addUIComponentToUpdateByAjax(uiPopup);
      }
    }
    public void execute(Event<UIAdminToolbarContainer> event) throws Exception {
      UIAdminToolbarContainer uicomp = event.getSource();
      UserNavigation edittedNavigation = Utils.getSelectedNavigation();

      WebuiRequestContext context = event.getRequestContext();
      UIApplication uiApplication = context.getUIApplication();

      if (edittedNavigation == null) {
        uiApplication.addMessage(
            new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
        return;
      }

      UserACL userACL = uicomp.getApplicationComponent(UserACL.class);
      if (edittedNavigation.getKey().getType().equals(SiteType.PORTAL)) {
        String portalName = Util.getPortalRequestContext().getPortalOwner();
        UserPortalConfigService configService =
            uicomp.getApplicationComponent(UserPortalConfigService.class);
        UserPortalConfig userPortalConfig =
            configService.getUserPortalConfig(
                portalName, context.getRemoteUser(), PortalRequestContext.USER_PORTAL_CONTEXT);
        if (userPortalConfig == null) {
          uiApplication.addMessage(
              new ApplicationMessage(
                  "UISiteManagement.msg.portal-not-exist", new String[] {portalName}));
          return;
        }
        if (!userACL.hasEditPermission(userPortalConfig.getPortalConfig())) {
          uiApplication.addMessage(
              new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
          return;
        }
      } else if (edittedNavigation.getKey().getType().equals(PortalConfig.GROUP_TYPE)) {
        if (!userACL.hasEditPermissionOnNavigation(
            SiteKey.group(edittedNavigation.getKey().getTypeName()))) {
          uiApplication.addMessage(
              new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
          return;
        }
      }

      if (uicomp.naviManager == null) {
        uicomp.naviManager = uicomp.createUIComponent(UINavigationManagement.class, null, null);
      }
      Utils.createPopupWindow(
          uicomp, uicomp.naviManager, EDIT_NAVIGATION_POPUP_CONTAINER_ID, 400, -1, -1);

      uicomp.naviManager.setSiteKey(edittedNavigation.getKey());
      UserPortal userPortal = getUserPortal();
      UINavigationNodeSelector selector =
          uicomp.naviManager.getChild(UINavigationNodeSelector.class);
      selector.setEdittedNavigation(edittedNavigation);
      selector.setUserPortal(userPortal);
      selector.initTreeData();

      context.addUIComponentToUpdateByAjax(uicomp);
    }
    public void execute(Event<UIAccountChangePass> event) throws Exception {
      UIAccountChangePass uiForm = event.getSource();
      OrganizationService service = uiForm.getApplicationComponent(OrganizationService.class);
      WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
      UIApplication uiApp = context.getUIApplication();
      String username = Util.getPortalRequestContext().getRemoteUser();
      User user = service.getUserHandler().findUserByName(username);
      String currentPass = uiForm.getUIStringInput("currentpass").getValue();
      String newPass = uiForm.getUIStringInput("newpass").getValue();
      String confirmnewPass = uiForm.getUIStringInput("confirmnewpass").getValue();

      Authenticator authenticator = uiForm.getApplicationComponent(Authenticator.class);
      boolean authenticated;
      try {
        UsernameCredential usernameCred = new UsernameCredential(username);
        PasswordCredential passwordCred = new PasswordCredential(currentPass);
        authenticator.validateUser(new Credential[] {usernameCred, passwordCred});
        authenticated = true;
      } catch (Exception ex) {
        authenticated = false;
      }

      if (!authenticated) {
        uiApp.addMessage(
            new ApplicationMessage(
                "UIAccountChangePass.msg.currentpassword-is-not-match", null, 1));
        uiForm.reset();
        event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
        return;
      }

      if (!newPass.equals(confirmnewPass)) {
        uiApp.addMessage(
            new ApplicationMessage("UIAccountChangePass.msg.password-is-not-match", null, 1));
        uiForm.reset();
        event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
        return;
      }
      try {
        user.setPassword(newPass);
        service.getUserHandler().saveUser(user, true);
        uiApp.addMessage(
            new ApplicationMessage("UIAccountChangePass.msg.change.pass.success", null));
        UIAccountSetting ui = uiForm.getParent();
        ui.getChild(UIAccountProfiles.class).setRendered(true);
        ui.getChild(UIAccountChangePass.class).setRendered(false);
        event.getRequestContext().addUIComponentToUpdateByAjax(ui);
      } catch (Exception e) {
        uiApp.addMessage(
            new ApplicationMessage(
                "UIAccountChangePass.msg.change.pass.fail", null, ApplicationMessage.ERROR));
      }
      uiForm.reset();
      event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
      return;
    }
    public void execute(Event<UIPortlet> event) throws Exception {
      UIPortlet uiPortlet = event.getSource();
      UIPortalApplication uiApp = Util.getUIPortalApplication();
      UIMaskWorkspace uiMaskWS = uiApp.getChildById(UIPortalApplication.UI_MASK_WS_ID);
      UIPortletForm uiPortletForm = uiMaskWS.createUIComponent(UIPortletForm.class, null, null);

      if (uiPortletForm.setValues(uiPortlet) == false) {
        uiMaskWS.setUIComponent(null);
        WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
        context
            .getUIApplication()
            .addMessage(
                (new ApplicationMessage(
                    "UIPortlet.message.portletDeleted", null, ApplicationMessage.ERROR)));
      } else {
        uiMaskWS.setUpdated(true);
        uiMaskWS.setWindowSize(800, -1);
        event.getRequestContext().addUIComponentToUpdateByAjax(uiMaskWS);
      }
    }
    public void execute(Event<UISocialGroupSelector> event) throws Exception {
      UISocialGroupSelector uiSelector = event.getSource();
      UIComponent uiPermission = uiSelector.<UIComponent>getParent().getParent();
      WebuiRequestContext pcontext = event.getRequestContext();

      UIPopupWindow uiPopup = uiSelector.getParent();
      UIForm uiForm = event.getSource().getAncestorOfType(UIForm.class);
      if (uiForm != null) {
        event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
      } else {
        event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup);
      }
      if (uiSelector.getCurrentGroup() == null) {
        UIApplication uiApp = pcontext.getUIApplication();
        uiApp.addMessage(new ApplicationMessage("UIGroupSelector.msg.selectGroup", null));
        // pcontext.addUIComponentToUpdateByAjax(uiApp.getUIPopupMessages());
        uiPopup.setShow(true);
        return;
      }

      uiPermission.broadcast(event, event.getExecutionPhase());
      uiPopup.setShow(false);
    }
  @Override
  public void onPostActivity(
      PostContext postContext,
      UIComponent source,
      WebuiRequestContext requestContext,
      String postedMessage)
      throws Exception {
    if (!isDocumentReady) {
      requestContext
          .getUIApplication()
          .addMessage(
              new ApplicationMessage(
                  "UIComposer.msg.error.Must_select_file", null, ApplicationMessage.INFO));
    } else {
      Map<String, String> activityParams = new LinkedHashMap<String, String>();
      Node node = getDocNode(REPOSITORY, WORKSPACE, documentPath);

      activityParams.put(UIDocActivity.DOCNAME, documentName);
      activityParams.put(UIDocActivity.DOCLINK, documentRefLink);
      activityParams.put(UIDocActivity.DOCPATH, documentPath);
      activityParams.put(UIDocActivity.REPOSITORY, REPOSITORY);
      activityParams.put(UIDocActivity.WORKSPACE, WORKSPACE);
      activityParams.put(UIDocActivity.MESSAGE, postedMessage);
      activityParams.put(
          BaseActivityProcessorPlugin.TEMPLATE_PARAM_TO_PROCESS, UIDocActivity.MESSAGE);

      if (node.getPrimaryNodeType().getName().equals(NodetypeConstant.NT_FILE)) {
        String activityOwnerId = UIDocActivity.getActivityOwnerId(node);
        DateFormat dateFormatter = null;
        dateFormatter = new SimpleDateFormat(ISO8601.SIMPLE_DATETIME_FORMAT);

        String illustrationImg = UIDocActivity.getIllustrativeImage(node);
        String strDateCreated = "";
        if (node.hasProperty(NodetypeConstant.EXO_DATE_CREATED)) {
          Calendar dateCreated = node.getProperty(NodetypeConstant.EXO_DATE_CREATED).getDate();
          strDateCreated = dateFormatter.format(dateCreated.getTime());
        }
        String strLastModified = "";
        if (node.hasNode(NodetypeConstant.JCR_CONTENT)) {
          Node contentNode = node.getNode(NodetypeConstant.JCR_CONTENT);
          if (contentNode.hasProperty(NodetypeConstant.JCR_LAST_MODIFIED)) {
            Calendar lastModified =
                contentNode.getProperty(NodetypeConstant.JCR_LAST_MODIFIED).getDate();
            strLastModified = dateFormatter.format(lastModified.getTime());
          }
        }

        activityParams.put(
            UIDocActivity.ID,
            node.isNodeType(NodetypeConstant.MIX_REFERENCEABLE) ? node.getUUID() : "");
        activityParams.put(UIDocActivity.CONTENT_NAME, node.getName());
        activityParams.put(UIDocActivity.AUTHOR, activityOwnerId);
        activityParams.put(UIDocActivity.DATE_CREATED, strDateCreated);
        activityParams.put(UIDocActivity.LAST_MODIFIED, strLastModified);
        activityParams.put(UIDocActivity.CONTENT_LINK, UIDocActivity.getContentLink(node));
        activityParams.put(
            UIDocActivity.ID,
            node.isNodeType(NodetypeConstant.MIX_REFERENCEABLE) ? node.getUUID() : "");
        activityParams.put(UIDocActivity.MIME_TYPE, UIDocActivity.getMimeType(node));
        activityParams.put(UIDocActivity.IMAGE_PATH, illustrationImg);
      }

      if (postContext == UIComposer.PostContext.SPACE) {
        postActivityToSpace(source, requestContext, activityParams);
      } else if (postContext == UIComposer.PostContext.USER) {
        postActivityToUser(source, requestContext, activityParams);
      }
    }
    resetValues();
  }
    public void execute(Event<UIForgetPassword> event) throws Exception {
      UIForgetPassword uiForm = event.getSource();
      UILogin uilogin = uiForm.getParent();
      WebuiRequestContext requestContext = event.getRequestContext();
      PortalRequestContext portalContext = PortalRequestContext.getCurrentInstance();
      String url = portalContext.getRequest().getRequestURL().toString();
      MailService mailSrc = uiForm.getApplicationComponent(MailService.class);
      OrganizationService orgSrc = uiForm.getApplicationComponent(OrganizationService.class);
      String userName = uiForm.getUIStringInput(Username).getValue();
      String email = uiForm.getUIStringInput(Email).getValue();
      uiForm.reset();

      User user = null;

      String tokenId = null;

      // User provided his username
      if (userName != null) {
        user = orgSrc.getUserHandler().findUserByName(userName);
        if (user == null) {
          requestContext
              .getUIApplication()
              .addMessage(new ApplicationMessage("UIForgetPassword.msg.user-not-exist", null));
          return;
        }
      }

      // User provided his email address
      if (user == null && email != null) {
        Query query = new Query();
        // Querying on email won't work. PLIDM-12
        // Note that querying on email is inefficient as it loops over all users...
        query.setEmail(email);
        PageList<User> users = orgSrc.getUserHandler().findUsers(query);
        if (users.getAll().size() > 0) {
          user = users.getAll().get(0);
        } else {
          requestContext
              .getUIApplication()
              .addMessage(new ApplicationMessage("UIForgetPassword.msg.email-not-exist", null));
          return;
        }
      }

      email = user.getEmail();

      // Create token
      RemindPasswordTokenService tokenService =
          uiForm.getApplicationComponent(RemindPasswordTokenService.class);
      Credentials credentials = new Credentials(user.getUserName(), "");
      tokenId = tokenService.createToken(credentials);

      String portalName = URLEncoder.encode(Util.getUIPortal().getName(), "UTF-8");

      ResourceBundle res = requestContext.getApplicationResourceBundle();
      String headerMail = "headermail";
      String footerMail = "footer";
      try {
        headerMail =
            res.getString(uiForm.getId() + ".mail.header")
                + "\n\n"
                + res.getString(uiForm.getId() + ".mail.user")
                + user.getUserName()
                + "\n"
                + res.getString(uiForm.getId() + ".mail.link");
        footerMail = "\n\n\n" + res.getString(uiForm.getId() + ".mail.footer");
      } catch (MissingResourceException e) {
        e.printStackTrace();
      }
      String host = url.substring(0, url.indexOf(requestContext.getRequestContextPath()));
      String activeLink =
          host
              + requestContext.getRequestContextPath()
              + "/public/"
              + portalName
              + "?portal:componentId=UIPortal&portal:action=RecoveryPasswordAndUsername&tokenId="
              + tokenId;
      String mailText = headerMail + "\n" + activeLink + footerMail;
      try {
        mailSrc.sendMessage(
            res.getString("UIForgetPassword.mail.from"),
            email,
            res.getString("UIForgetPassword.mail.subject"),
            mailText);
      } catch (Exception e) {
        requestContext
            .getUIApplication()
            .addMessage(new ApplicationMessage("UIForgetPassword.msg.send-mail-fail", null));
        requestContext.addUIComponentToUpdateByAjax(uilogin);

        return;
      }

      uilogin.getChild(UILoginForm.class).setRendered(true);
      uilogin.getChild(UIForgetPasswordWizard.class).setRendered(false);
      uilogin.getChild(UIForgetPassword.class).setRendered(false);
      requestContext
          .getUIApplication()
          .addMessage(new ApplicationMessage("UIForgetPassword.msg.send-mail-success", null));
      requestContext.addUIComponentToUpdateByAjax(uilogin);
    }