You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ri...@apache.org on 2022/01/02 10:37:04 UTC

[incubator-streampipes] branch dev updated: [STREAMPIPES-496] Properly update welcome tour display status

This is an automated email from the ASF dual-hosted git repository.

riemer pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new ebf2a0b  [STREAMPIPES-496] Properly update welcome tour display status
     new ea247eb  Merge branch 'dev' of github.com:apache/incubator-streampipes into dev
ebf2a0b is described below

commit ebf2a0be43ee17f5acb12054284c31f030fdeefe
Author: Dominik Riemer <do...@gmail.com>
AuthorDate: Sun Jan 2 11:35:52 2022 +0100

    [STREAMPIPES-496] Properly update welcome tour display status
---
 .../backend/StreamPipesResourceConfig.java         |  1 -
 .../apache/streampipes/rest/impl/UserResource.java | 25 +++++++++++++++---
 .../user/management/jwt/JwtTokenProvider.java      | 14 +++-------
 .../user/management/util/UserInfoUtil.java         | 20 ++++++++++-----
 .../welcome-tour/welcome-tour.component.html       |  2 +-
 .../dialog/welcome-tour/welcome-tour.component.ts  | 30 ++++++++++++++++------
 ui/src/app/editor/editor.component.ts              |  2 +-
 ui/src/app/services/auth.service.ts                | 10 +++++---
 ui/src/app/services/rest-api.service.ts            |  4 ---
 9 files changed, 70 insertions(+), 38 deletions(-)

diff --git a/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java b/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
index 4dec274..fd3c485 100644
--- a/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
+++ b/streampipes-backend/src/main/java/org/apache/streampipes/backend/StreamPipesResourceConfig.java
@@ -96,7 +96,6 @@ public class StreamPipesResourceConfig extends ResourceConfig {
         register(Setup.class);
         register(ResetResource.class);
         register(RestorePasswordResource.class);
-        register(UserProfile.class);
         register(UserResource.class);
         register(Version.class);
         register(PipelineElementAsset.class);
diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
index 0f41353..56a745f 100644
--- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
+++ b/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserResource.java
@@ -187,7 +187,7 @@ public class UserResource extends AbstractAuthGuardedRestResource {
     String authenticatedUserId = getAuthenticatedUsername();
     if (user != null && (authenticatedUserId.equals(principalId) || isAdmin())) {
       Principal existingUser = getPrincipalById(principalId);
-      updateUser((UserAccount) existingUser, user);
+      updateUser((UserAccount) existingUser, user, isAdmin());
       user.setRev(existingUser.getRev());
       getUserStorage().updateUser(user);
       return ok(Notifications.success("User updated"));
@@ -206,6 +206,9 @@ public class UserResource extends AbstractAuthGuardedRestResource {
     if (user != null && (authenticatedUserId.equals(principalId) || isAdmin())) {
       Principal existingUser = getPrincipalById(principalId);
       user.setRev(existingUser.getRev());
+      if (!isAdmin()) {
+        replacePermissions(user, existingUser);
+      }
       if (!user.isSecretEncrypted()) {
         user.setClientSecret(SecretEncryptionManager.encrypt(user.getClientSecret()));
         user.setSecretEncrypted(true);
@@ -218,11 +221,21 @@ public class UserResource extends AbstractAuthGuardedRestResource {
   }
 
   private boolean isAdmin() {
-    return SecurityContextHolder.getContext().getAuthentication().getAuthorities().stream().anyMatch(r -> r.getAuthority().equals(Role.ROLE_ADMIN.name()));
+    return SecurityContextHolder
+            .getContext()
+            .getAuthentication()
+            .getAuthorities()
+            .stream()
+            .anyMatch(r -> r.getAuthority().equals(Role.ROLE_ADMIN.name()));
   }
 
-  private void updateUser(UserAccount existingUser, UserAccount user) {
+  private void updateUser(UserAccount existingUser,
+                          UserAccount user,
+                          boolean adminPrivileges) {
     user.setPassword(existingUser.getPassword());
+    if (!adminPrivileges) {
+      replacePermissions(user, existingUser);
+    }
     user.setUserApiTokens(existingUser
             .getUserApiTokens()
             .stream()
@@ -262,4 +275,10 @@ public class UserResource extends AbstractAuthGuardedRestResource {
       ((UserAccount) principal).setPassword("");
     }
   }
+
+  private void replacePermissions(Principal principal, Principal existingPrincipal) {
+    principal.setGroups(existingPrincipal.getGroups());
+    principal.setObjectPermissions(existingPrincipal.getObjectPermissions());
+    principal.setRoles(existingPrincipal.getRoles());
+  }
 }
diff --git a/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/JwtTokenProvider.java b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/JwtTokenProvider.java
index 9221be4..4cba6f8 100644
--- a/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/JwtTokenProvider.java
+++ b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/jwt/JwtTokenProvider.java
@@ -22,10 +22,10 @@ import org.apache.streampipes.config.backend.BackendConfig;
 import org.apache.streampipes.config.backend.model.LocalAuthConfig;
 import org.apache.streampipes.model.client.user.Principal;
 import org.apache.streampipes.model.client.user.UserAccount;
-import org.apache.streampipes.model.client.user.UserInfo;
 import org.apache.streampipes.security.jwt.JwtTokenUtils;
 import org.apache.streampipes.user.management.model.PrincipalUserDetails;
 import org.apache.streampipes.user.management.util.GrantedAuthoritiesBuilder;
+import org.apache.streampipes.user.management.util.UserInfoUtil;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.GrantedAuthority;
 
@@ -73,7 +73,7 @@ public class JwtTokenProvider {
 	private Map<String, Object> makeClaims(Principal principal,
 																				 Set<String> roles) {
 		Map<String, Object> claims = new HashMap<>();
-		claims.put(CLAIM_USER, toUserInfo((UserAccount) principal, roles));
+		claims.put(CLAIM_USER, UserInfoUtil.toUserInfo((UserAccount) principal, roles));
 
 		return claims;
 	}
@@ -104,13 +104,5 @@ public class JwtTokenProvider {
 		return new Date(now.getTime() + authConfig().getTokenExpirationTimeMillis());
 	}
 
-	private UserInfo toUserInfo(UserAccount localUser,
-															Set<String> roles) {
-		UserInfo userInfo = new UserInfo();
-		userInfo.setUsername(localUser.getUsername());
-		userInfo.setDisplayName(localUser.getUsername());
-		userInfo.setShowTutorial(!localUser.isHideTutorial());
-		userInfo.setRoles(roles);
-		return userInfo;
-	}
+
 }
diff --git a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserProfile.java b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/util/UserInfoUtil.java
similarity index 57%
rename from streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserProfile.java
rename to streampipes-user-management/src/main/java/org/apache/streampipes/user/management/util/UserInfoUtil.java
index d3f2de2..2cc2df1 100644
--- a/streampipes-rest/src/main/java/org/apache/streampipes/rest/impl/UserProfile.java
+++ b/streampipes-user-management/src/main/java/org/apache/streampipes/user/management/util/UserInfoUtil.java
@@ -16,14 +16,22 @@
  *
  */
 
-package org.apache.streampipes.rest.impl;
+package org.apache.streampipes.user.management.util;
 
-import org.apache.streampipes.rest.core.base.impl.AbstractAuthGuardedRestResource;
+import org.apache.streampipes.model.client.user.UserAccount;
+import org.apache.streampipes.model.client.user.UserInfo;
 
-import javax.ws.rs.Path;
-
-@Path("/v2/users/profile")
-public class UserProfile extends AbstractAuthGuardedRestResource {
+import java.util.Set;
 
+public class UserInfoUtil {
 
+  public static UserInfo toUserInfo(UserAccount userAccount,
+                              Set<String> roles) {
+    UserInfo userInfo = new UserInfo();
+    userInfo.setUsername(userAccount.getUsername());
+    userInfo.setDisplayName(userAccount.getUsername());
+    userInfo.setShowTutorial(!userAccount.isHideTutorial());
+    userInfo.setRoles(roles);
+    return userInfo;
+  }
 }
diff --git a/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.html b/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.html
index cd7dcc9..1bd25ee 100644
--- a/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.html
+++ b/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.html
@@ -30,7 +30,7 @@
                 <b>{{appConstants.APP_NAME}} is your self-service toolbox for real-time analytics.</b>
                 <p>If you are using {{appConstants.APP_NAME}} for the first time, we recommend taking the interactive tour!</p>
             </div>
-            <button mat-button mat-raised-button color="primary" (click)="startCreatePipelineTour()">Start Tour</button>
+            <button mat-button mat-raised-button color="accent" (click)="startCreatePipelineTour()">Start Tour</button>
         </div>
     </div>
     <mat-divider></mat-divider>
diff --git a/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.ts b/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.ts
index 9793c0d..d381bc2 100644
--- a/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.ts
+++ b/ui/src/app/editor/dialog/welcome-tour/welcome-tour.component.ts
@@ -17,34 +17,47 @@
  */
 
 import { DialogRef } from "../../../core-ui/dialog/base-dialog/dialog-ref";
-import { RestApi } from "../../../services/rest-api.service";
 import { ShepherdService } from "../../../services/tour/shepherd.service";
-import { Component } from "@angular/core";
+import { Component, Input, OnInit } from "@angular/core";
 import { AppConstants } from "../../../services/app.constants";
+import { AuthService } from "../../../services/auth.service";
+import { UserAccount, UserInfo } from "../../../core-model/gen/streampipes-model-client";
+import { ProfileService } from "../../../profile/profile.service";
 
 @Component({
   selector: 'welcome-tour',
   templateUrl: './welcome-tour.component.html',
   styleUrls: ['./welcome-tour.component.scss']
 })
-export class WelcomeTourComponent {
+export class WelcomeTourComponent implements OnInit {
 
-  user: any;
+  @Input()
+  userInfo: UserInfo;
 
-  constructor(private DialogRef: DialogRef<WelcomeTourComponent>,
-              private RestApi: RestApi,
+  currentUser: UserAccount;
+
+  constructor(private authService: AuthService,
+              private DialogRef: DialogRef<WelcomeTourComponent>,
+              private profileService : ProfileService,
               private ShepherdService: ShepherdService,
               public appConstants: AppConstants) {
   }
 
+  ngOnInit(): void {
+    this.profileService.getUserProfile(this.userInfo.username).subscribe(data => {
+      this.currentUser = data;
+    })
+  }
+
   startCreatePipelineTour() {
     this.ShepherdService.startCreatePipelineTour();
     this.close();
   }
 
   hideTourForever() {
-    this.user.hideTutorial = true;
-    this.RestApi.updateUserDetails(this.user).subscribe(data => {
+    this.currentUser.hideTutorial = true;
+    this.profileService.updateUserProfile(this.currentUser).subscribe(data => {
+      this.authService.updateTokenAndUserInfo();
       this.close();
     });
   }
@@ -52,4 +65,5 @@ export class WelcomeTourComponent {
   close() {
     this.DialogRef.close();
   }
+
 }
diff --git a/ui/src/app/editor/editor.component.ts b/ui/src/app/editor/editor.component.ts
index 241cfb7..a872da1 100644
--- a/ui/src/app/editor/editor.component.ts
+++ b/ui/src/app/editor/editor.component.ts
@@ -154,7 +154,7 @@ export class EditorComponent implements OnInit {
                     panelType: PanelType.STANDARD_PANEL,
                     title: 'Welcome to StreamPipes',
                     data: {
-                        'user': currentUser.displayName
+                        'userInfo': currentUser
                     }
                 });
             }
diff --git a/ui/src/app/services/auth.service.ts b/ui/src/app/services/auth.service.ts
index 0ad1bad..2198414 100644
--- a/ui/src/app/services/auth.service.ts
+++ b/ui/src/app/services/auth.service.ts
@@ -101,13 +101,17 @@ export class AuthService {
             switchMap((expiresIn: Date) => timer(expiresIn.getTime() - Date.now() - 60000)),
         ).subscribe(() => {
             if (this.authenticated()) {
-                this.loginService.renewToken().subscribe(data => {
-                    this.login(data);
-                });
+                this.updateTokenAndUserInfo();
             }
         });
     }
 
+    updateTokenAndUserInfo() {
+        this.loginService.renewToken().subscribe(data => {
+            this.login(data);
+        })
+    }
+
     watchTokenExpiration() {
         this.authToken$.pipe(
             filter((token: any) => !!token),
diff --git a/ui/src/app/services/rest-api.service.ts b/ui/src/app/services/rest-api.service.ts
index f537eeb..d88829e 100644
--- a/ui/src/app/services/rest-api.service.ts
+++ b/ui/src/app/services/rest-api.service.ts
@@ -45,10 +45,6 @@ export class RestApi {
         return this.getServerUrl() + '/pe/' + appId + '/assets';
     }
 
-    updateUserDetails(user) {
-        return this.$http.put(this.urlApiBase() + '/users/profile', user);
-    }
-
     configured(): Observable<any> {
         return this.$http.get(this.getServerUrl() + '/setup/configured', {
             headers: { ignoreLoadingBar: '' }