Commit 84b0a8e24d5375248b19318b7334a6fc9debb031

Authored by Igor Kulikov
Committed by GitHub
2 parents a16e5319 32e1c0d5

Merge pull request #333 from eykamp/is_public

Centralize checking for whether customer is public
... ... @@ -59,11 +59,7 @@ public class CustomerController extends BaseController {
59 59 ObjectMapper objectMapper = new ObjectMapper();
60 60 ObjectNode infoObject = objectMapper.createObjectNode();
61 61 infoObject.put("title", customer.getTitle());
62   - boolean isPublic = false;
63   - if (customer.getAdditionalInfo() != null && customer.getAdditionalInfo().has(IS_PUBLIC)) {
64   - isPublic = customer.getAdditionalInfo().get(IS_PUBLIC).asBoolean();
65   - }
66   - infoObject.put(IS_PUBLIC, isPublic);
  62 + infoObject.put(IS_PUBLIC, customer.isPublic());
67 63 return infoObject;
68 64 } catch (Exception e) {
69 65 throw handleException(e);
... ...
... ... @@ -103,13 +103,11 @@ public class RefreshTokenAuthenticationProvider implements AuthenticationProvide
103 103 if (publicCustomer == null) {
104 104 throw new UsernameNotFoundException("Public entity not found by refresh token");
105 105 }
106   - boolean isPublic = false;
107   - if (publicCustomer.getAdditionalInfo() != null && publicCustomer.getAdditionalInfo().has("isPublic")) {
108   - isPublic = publicCustomer.getAdditionalInfo().get("isPublic").asBoolean();
109   - }
110   - if (!isPublic) {
  106 +
  107 + if (!publicCustomer.isPublic()) {
111 108 throw new BadCredentialsException("Refresh token is not valid");
112 109 }
  110 +
113 111 User user = new User(new UserId(UUIDBased.EMPTY));
114 112 user.setTenantId(publicCustomer.getTenantId());
115 113 user.setCustomerId(publicCustomer.getId());
... ...
... ... @@ -108,11 +108,7 @@ public class RestAuthenticationProvider implements AuthenticationProvider {
108 108 if (publicCustomer == null) {
109 109 throw new UsernameNotFoundException("Public entity not found: " + publicId);
110 110 }
111   - boolean isPublic = false;
112   - if (publicCustomer.getAdditionalInfo() != null && publicCustomer.getAdditionalInfo().has("isPublic")) {
113   - isPublic = publicCustomer.getAdditionalInfo().get("isPublic").asBoolean();
114   - }
115   - if (!isPublic) {
  111 + if (!publicCustomer.isPublic()) {
116 112 throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
117 113 }
118 114 User user = new User(new UserId(UUIDBased.EMPTY));
... ...
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.common.data;
17 17
  18 +import com.fasterxml.jackson.annotation.JsonIgnore;
18 19 import com.fasterxml.jackson.annotation.JsonProperty;
19 20 import com.fasterxml.jackson.annotation.JsonProperty.Access;
20 21 import org.thingsboard.server.common.data.id.CustomerId;
... ... @@ -61,6 +62,15 @@ public class Customer extends ContactBased<CustomerId> implements HasName {
61 62 this.title = title;
62 63 }
63 64
  65 + @JsonIgnore
  66 + public boolean isPublic() {
  67 + if (getAdditionalInfo() != null && getAdditionalInfo().has("isPublic")) {
  68 + return additionalInfo.get("isPublic").asBoolean();
  69 + }
  70 +
  71 + return false;
  72 + }
  73 +
64 74 @Override
65 75 @JsonProperty(access = Access.READ_ONLY)
66 76 public String getName() {
... ... @@ -149,5 +159,4 @@ public class Customer extends ContactBased<CustomerId> implements HasName {
149 159 builder.append("]");
150 160 return builder.toString();
151 161 }
152   -
153 162 }
... ...