Skip to content

Instantly share code, notes, and snippets.

@buildmotion
Last active February 14, 2018 17:47
Show Gist options
  • Save buildmotion/8985dfe4c26433c3f79e3b6b2f12647e to your computer and use it in GitHub Desktop.
Save buildmotion/8985dfe4c26433c3f79e3b6b2f12647e to your computer and use it in GitHub Desktop.
ValidateCredentialsAction
import { Observable } from 'rxjs/Observable';
import { ErrorObservable } from 'rxjs/Observable/ErrorObservable';
import 'rxjs/add/observable/throw';
import { Response } from '@angular/http';
import * as rules from 'angular-rules-engine';
import { ActionResult } from 'angular-actions';
import { SecurityActionBase } from './security-action-base.action';
import { Severity } from 'nspire-logging/severity.enum';
import { Credentials } from './../../models/credentials.model';
/**
* Use this action to retrieve [Service Information].
*/
export class ValidateCredentialsAction extends SecurityActionBase {
response: Observable<Response>;
constructor(private credentials: Credentials) {
super();
this.actionName = 'ValidateCredentialsAction ';
console.log(`Running the [${this.actionName}] constructor.`);
}
/**
* Override this method from the base [Action] class to allow for rules to be added to the
* action's [ValidationContext]. Any rules added to the [ValidationContext] here will be executed when
* the action's [ValidateAction] method is called - this method is just one of many pipeline methods
* of the [Action] framework.
*/
preValidateAction() {
console.log(`Running the [preValidateAction] for the ${this.actionName} action.`);
let showValidationErrors = true;
this.validationContext.addRule(new rules.IsNotNullOrUndefined(
'CredentialsAreValid',
'A valid username and password is required.',
this.credentials,
showValidationErrors
));
if (this.credentials) {
this.validationContext.addRule(new rules.StringIsNotNullEmptyRange(
'UserIsValid',
'A valid username and password is required.',
this.credentials.UserName,
1,
64,
showValidationErrors
));
}
if (this.credentials) {
this.validationContext.addRule(new rules.StringIsNotNullEmptyRange(
'PasswordIsValid',
'A valid username and password is required.',
this.credentials.Password,
1,
64,
showValidationErrors
));
}
}
/**
* Use this method to execute business logic - this method is allowed to execute only if the current action
* does not contain any rule violations.
*/
performAction() {
console.log(`Running the [performAction] for the ${this.actionName}.`);
this.response = this.businessProvider.securityHttpService.validateCredentials(this.credentials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment