Skip to content

Commit

Permalink
Validate tenant id in API and UI. #531
Browse files Browse the repository at this point in the history
  • Loading branch information
derekadams committed Aug 17, 2017
1 parent 365a2a2 commit 4afc933
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Expand Up @@ -80,6 +80,9 @@ public enum ErrorCode {
/** Indicates 'stop' command issued to tenant that was already stopped */
TenantAlreadyStopped(301, "Tenant was already stopped."),

/** Indicates a tenant id was passed with the wrong format */
TenantIdFormat(305, "Tenant id should be an alphanumeric value with no spaces."),

/***************************
* INVALID OR DUPLICATE ID *
***************************/
Expand Down
Expand Up @@ -188,6 +188,19 @@ protected static void requireNotNull(Object field) throws SiteWhereException {
}
}

/**
* Requires that a String field be a non null, non space-filled value.
*
* @param field
* @throws SiteWhereException
*/
protected static void requireFormat(String field, String regex, ErrorCode ifFails) throws SiteWhereException {
require(field);
if (!field.matches(regex)) {
throw new SiteWhereSystemException(ifFails, ErrorLevel.ERROR);
}
}

/**
* Detect whether the request has an updated value.
*
Expand Down Expand Up @@ -1448,7 +1461,7 @@ public static Tenant tenantCreateLogic(ITenantCreateRequest request) throws Site
Tenant tenant = new Tenant();

// Id is required.
require(request.getId());
requireFormat(request.getId(), "[\\w]*", ErrorCode.TenantIdFormat);
tenant.setId(request.getId());

// Name is required.
Expand Down
9 changes: 8 additions & 1 deletion sitewhere-ui/src/components/tenants/TenantDialog.vue
Expand Up @@ -20,7 +20,8 @@
<v-layout row wrap>
<v-flex xs12>
<v-text-field required class="mt-1" label="Tenant id"
v-model="tenantId" hide-details prepend-icon="info">
v-model="tenantId" hide-details prepend-icon="info"
:rules="[rules.tenantId]">
</v-text-field>
</v-flex>
<v-flex xs12>
Expand Down Expand Up @@ -86,6 +87,12 @@ export default {
metadata: [],
templatesList: [],
allUsers: [],
rules: {
tenantId: (value) => {
const pattern = /^[\w]*$/
return pattern.test(value) || 'Tenant id should be alphanumeric with no spaces.'
}
},
error: null
}),
Expand Down

0 comments on commit 4afc933

Please sign in to comment.