|
@@ -15,6 +15,9 @@ |
|
@@ -15,6 +15,9 @@ |
15
|
*/
|
15
|
*/
|
16
|
package org.thingsboard.server.service.security.auth.oauth2;
|
16
|
package org.thingsboard.server.service.security.auth.oauth2;
|
17
|
|
17
|
|
|
|
18
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
19
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
20
|
+import com.google.common.base.Strings;
|
18
|
import lombok.extern.slf4j.Slf4j;
|
21
|
import lombok.extern.slf4j.Slf4j;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
23
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
@@ -22,14 +25,21 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
@@ -22,14 +25,21 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; |
22
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
25
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
23
|
import org.springframework.util.StringUtils;
|
26
|
import org.springframework.util.StringUtils;
|
24
|
import org.thingsboard.server.common.data.Customer;
|
27
|
import org.thingsboard.server.common.data.Customer;
|
|
|
28
|
+import org.thingsboard.server.common.data.DashboardInfo;
|
25
|
import org.thingsboard.server.common.data.Tenant;
|
29
|
import org.thingsboard.server.common.data.Tenant;
|
26
|
import org.thingsboard.server.common.data.User;
|
30
|
import org.thingsboard.server.common.data.User;
|
27
|
import org.thingsboard.server.common.data.id.CustomerId;
|
31
|
import org.thingsboard.server.common.data.id.CustomerId;
|
|
|
32
|
+import org.thingsboard.server.common.data.id.DashboardId;
|
|
|
33
|
+import org.thingsboard.server.common.data.id.IdBased;
|
28
|
import org.thingsboard.server.common.data.id.TenantId;
|
34
|
import org.thingsboard.server.common.data.id.TenantId;
|
|
|
35
|
+import org.thingsboard.server.common.data.page.TextPageData;
|
29
|
import org.thingsboard.server.common.data.page.TextPageLink;
|
36
|
import org.thingsboard.server.common.data.page.TextPageLink;
|
|
|
37
|
+import org.thingsboard.server.common.data.page.TimePageData;
|
|
|
38
|
+import org.thingsboard.server.common.data.page.TimePageLink;
|
30
|
import org.thingsboard.server.common.data.security.Authority;
|
39
|
import org.thingsboard.server.common.data.security.Authority;
|
31
|
import org.thingsboard.server.common.data.security.UserCredentials;
|
40
|
import org.thingsboard.server.common.data.security.UserCredentials;
|
32
|
import org.thingsboard.server.dao.customer.CustomerService;
|
41
|
import org.thingsboard.server.dao.customer.CustomerService;
|
|
|
42
|
+import org.thingsboard.server.dao.dashboard.DashboardService;
|
33
|
import org.thingsboard.server.dao.oauth2.OAuth2User;
|
43
|
import org.thingsboard.server.dao.oauth2.OAuth2User;
|
34
|
import org.thingsboard.server.dao.tenant.TenantService;
|
44
|
import org.thingsboard.server.dao.tenant.TenantService;
|
35
|
import org.thingsboard.server.dao.user.UserService;
|
45
|
import org.thingsboard.server.dao.user.UserService;
|
|
@@ -40,11 +50,15 @@ import org.thingsboard.server.service.security.model.UserPrincipal; |
|
@@ -40,11 +50,15 @@ import org.thingsboard.server.service.security.model.UserPrincipal; |
40
|
import java.io.IOException;
|
50
|
import java.io.IOException;
|
41
|
import java.util.List;
|
51
|
import java.util.List;
|
42
|
import java.util.Optional;
|
52
|
import java.util.Optional;
|
|
|
53
|
+import java.util.concurrent.ExecutionException;
|
43
|
import java.util.concurrent.locks.Lock;
|
54
|
import java.util.concurrent.locks.Lock;
|
44
|
import java.util.concurrent.locks.ReentrantLock;
|
55
|
import java.util.concurrent.locks.ReentrantLock;
|
45
|
|
56
|
|
46
|
@Slf4j
|
57
|
@Slf4j
|
47
|
public abstract class AbstractOAuth2ClientMapper {
|
58
|
public abstract class AbstractOAuth2ClientMapper {
|
|
|
59
|
+ private static final int DASHBOARDS_REQUEST_LIMIT = 10;
|
|
|
60
|
+
|
|
|
61
|
+ private static final ObjectMapper objectMapper = new ObjectMapper();
|
48
|
|
62
|
|
49
|
@Autowired
|
63
|
@Autowired
|
50
|
private UserService userService;
|
64
|
private UserService userService;
|
|
@@ -59,6 +73,9 @@ public abstract class AbstractOAuth2ClientMapper { |
|
@@ -59,6 +73,9 @@ public abstract class AbstractOAuth2ClientMapper { |
59
|
private CustomerService customerService;
|
73
|
private CustomerService customerService;
|
60
|
|
74
|
|
61
|
@Autowired
|
75
|
@Autowired
|
|
|
76
|
+ private DashboardService dashboardService;
|
|
|
77
|
+
|
|
|
78
|
+ @Autowired
|
62
|
private InstallScripts installScripts;
|
79
|
private InstallScripts installScripts;
|
63
|
|
80
|
|
64
|
private final Lock userCreationLock = new ReentrantLock();
|
81
|
private final Lock userCreationLock = new ReentrantLock();
|
|
@@ -92,6 +109,20 @@ public abstract class AbstractOAuth2ClientMapper { |
|
@@ -92,6 +109,20 @@ public abstract class AbstractOAuth2ClientMapper { |
92
|
user.setEmail(oauth2User.getEmail());
|
109
|
user.setEmail(oauth2User.getEmail());
|
93
|
user.setFirstName(oauth2User.getFirstName());
|
110
|
user.setFirstName(oauth2User.getFirstName());
|
94
|
user.setLastName(oauth2User.getLastName());
|
111
|
user.setLastName(oauth2User.getLastName());
|
|
|
112
|
+
|
|
|
113
|
+ if (!StringUtils.isEmpty(oauth2User.getDefaultDashboardName())) {
|
|
|
114
|
+ Optional<DashboardId> dashboardIdOpt =
|
|
|
115
|
+ user.getAuthority() == Authority.TENANT_ADMIN ?
|
|
|
116
|
+ getDashboardId(tenantId, oauth2User.getDefaultDashboardName())
|
|
|
117
|
+ : getDashboardId(tenantId, customerId, oauth2User.getDefaultDashboardName());
|
|
|
118
|
+ if (dashboardIdOpt.isPresent()) {
|
|
|
119
|
+ ObjectNode additionalInfo = objectMapper.createObjectNode();
|
|
|
120
|
+ additionalInfo.put("defaultDashboardFullscreen", oauth2User.isAlwaysFullScreen());
|
|
|
121
|
+ additionalInfo.put("defaultDashboardId", dashboardIdOpt.get().getId().toString());
|
|
|
122
|
+ user.setAdditionalInfo(additionalInfo);
|
|
|
123
|
+ }
|
|
|
124
|
+ }
|
|
|
125
|
+
|
95
|
user = userService.saveUser(user);
|
126
|
user = userService.saveUser(user);
|
96
|
if (activateUser) {
|
127
|
if (activateUser) {
|
97
|
UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getTenantId(), user.getId());
|
128
|
UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getTenantId(), user.getId());
|
|
@@ -143,4 +174,32 @@ public abstract class AbstractOAuth2ClientMapper { |
|
@@ -143,4 +174,32 @@ public abstract class AbstractOAuth2ClientMapper { |
143
|
return customerService.saveCustomer(customer).getId();
|
174
|
return customerService.saveCustomer(customer).getId();
|
144
|
}
|
175
|
}
|
145
|
}
|
176
|
}
|
|
|
177
|
+
|
|
|
178
|
+ private Optional<DashboardId> getDashboardId(TenantId tenantId, String dashboardName) {
|
|
|
179
|
+ TextPageLink searchTextLink = new TextPageLink(1, dashboardName);
|
|
|
180
|
+ TextPageData<DashboardInfo> dashboardsPage = dashboardService.findDashboardsByTenantId(tenantId, searchTextLink);
|
|
|
181
|
+ return dashboardsPage.getData().stream()
|
|
|
182
|
+ .findAny()
|
|
|
183
|
+ .map(IdBased::getId);
|
|
|
184
|
+ }
|
|
|
185
|
+
|
|
|
186
|
+ private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) {
|
|
|
187
|
+ TimePageData<DashboardInfo> dashboardsPage = null;
|
|
|
188
|
+ do {
|
|
|
189
|
+ TimePageLink timePageLink = dashboardsPage != null ?
|
|
|
190
|
+ dashboardsPage.getNextPageLink() : new TimePageLink(DASHBOARDS_REQUEST_LIMIT);
|
|
|
191
|
+ try {
|
|
|
192
|
+ dashboardsPage = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, timePageLink).get();
|
|
|
193
|
+ } catch (InterruptedException | ExecutionException e) {
|
|
|
194
|
+ throw new RuntimeException("Failed to get customer's dashboards.", e);
|
|
|
195
|
+ }
|
|
|
196
|
+ Optional<DashboardInfo> dashboardInfoOpt = dashboardsPage.getData().stream()
|
|
|
197
|
+ .filter(dashboardInfo -> dashboardName.equals(dashboardInfo.getName()))
|
|
|
198
|
+ .findAny();
|
|
|
199
|
+ if (dashboardInfoOpt.isPresent()) {
|
|
|
200
|
+ return dashboardInfoOpt.map(DashboardInfo::getId);
|
|
|
201
|
+ }
|
|
|
202
|
+ } while (dashboardsPage.hasNext());
|
|
|
203
|
+ return Optional.empty();
|
|
|
204
|
+ }
|
146
|
} |
205
|
} |