projects/wms-framework/src/lib/domainservicesupport/Authentication.ts
Properties |
Name |
Name:
|
Type : string
|
import {
LoginOperation,
LoadUserOperation,
LoadOperation,
} from './OperationsSupport';
import { SubscriptionEvent } from '../utils/SubscriptionEvent';
import { Debugger } from '../diagnostics/Debugger';
import { INotifyPropertyChanged } from '../basecomponentmodel/INotifyPropertyChanged';
import { DomainContext, LoadBehavior } from './DomainContext';
import { EntityQuery } from './EntitiesSupport';
import { iuFirst } from '../baseframework/collections';
/**
* Defines a base {@link AuthenticationService} oject responsible for doing the
* handshake with server service in order to get access to resoruces
*
* @wType System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationService
*/
export class AuthenticationService implements INotifyPropertyChanged {
#user: IPrincipal;
constructor() {
this.PropertyChanged = new SubscriptionEvent<
(o: any, args: { PropertyName: string }) => void
>();
}
/**
* Gets the user associated that was used to loging or as a result of loading
* it from service
* @wProperty User
*/
public get user(): IPrincipal {
return this.#user;
}
/**
* Sets the user for this authentication service, fires a property changed event
* @wProperty User
*/
public set user(value: IPrincipal) {
this.#user = value;
this.PropertyChanged.fire([this, { PropertyName: 'user', Value: value }]);
}
PropertyChanged: SubscriptionEvent<
(o: any, args: { PropertyName: string; Value: any }) => void
>;
/**
*
* @param arg0
* @param arg1
* @wNoMap
*/
Logout(arg0: (logoutOperation: any) => void, arg1: null): any {
Debugger.Throw('Method not implemented.');
}
/**
*
* @param arg0
* @param LoginOperation_Completed
* @param arg2
* @returns {LoginOperation}
* @wNoMap
*/
Login(
arg0: LoginParameters,
LoginOperation_Completed: (loginOperation: LoginOperation) => void,
arg2: null
): LoginOperation {
Debugger.Throw('Method not implemented.');
return null;
}
/**
* Loads the information of the user currently connected.
* @param callback a call back to be called once the user information is loaded.
* @param dataContext user specific information
*/
LoadUser(
callback: (operation: LoadUserOperation) => void,
dataContext: null
): any {
return null;
}
LoggedIn = new SubscriptionEvent<(sender: any, e: any) => void>();
LoggedOut = new SubscriptionEvent<(sender: any, e: any) => void>();
}
/**
* @wInterface System.Security.Principal.IPrincipal
*/
export interface IPrincipal {
Identity: IIdentity;
}
class PrincipalImpl implements IPrincipal {
Identity: IIdentity;
}
/**
* @wInterface System.Security.Principal.IIdentity
*/
export interface IIdentity {
Name: string;
}
/**
* @wType System.ServiceModel.DomainServices.Client.ApplicationServices.LoginParameters
*/
export class LoginParameters {
constructor(
private userName: string,
private password: string,
private persistent: boolean,
private customData?: string
) {}
}
/**
* @wType System.ServiceModel.DomainServices.Client.ApplicationServices.WebContextBase
*/
export class WebContextBase implements INotifyPropertyChanged {
private _authentication: AuthenticationService;
private authenticationChangeRef: (
o: any,
args: { PropertyName: string; Value: any }
) => void;
public get Authentication(): AuthenticationService {
return this._authentication;
}
public set Authentication(value: AuthenticationService) {
if (this._authentication !== undefined && this._authentication !== null) {
this._authentication.PropertyChanged.removeHandler(
this.authenticationChangeRef
);
}
this._authentication = value;
if (this._authentication !== undefined && this._authentication !== null) {
this.authenticationChangeRef =
this._authentication.PropertyChanged.addHandler((o, args) => {
if (args.PropertyName === 'user') {
this._user = <IPrincipal>args.Value;
}
});
}
}
/**
* @wProperty User
*/
public _user: IPrincipal;
public static Current: WebContextBase = new WebContextBase();
public constructor() {
this._user = new PrincipalImpl();
}
PropertyChanged: SubscriptionEvent<
(o: any, args: { PropertyName: string }) => void
> = new SubscriptionEvent();
}
/**
* @wType System.ServiceModel.DomainServices.Client.ApplicationServices.WebAuthenticationService
*/
export class WebAuthenticationService extends AuthenticationService {
DomainContext: DomainContext;
// inherithed
LoadUser(
callback: (operation: LoadUserOperation) => void,
userState: any
): any {
let query: EntityQuery<unknown> = (
this.DomainContext as any
).GetUserQuery();
let loadOperation: LoadOperation<unknown> = this.DomainContext.Load(
query,
LoadBehavior.RefreshCurrent,
false,
query.Constructor
);
loadOperation.Completed = () => {
let userOperation = new LoadUserOperation();
try {
this.user = iuFirst(loadOperation.Entities) as IPrincipal;
userOperation.User = this.user;
callback(userOperation);
} catch (error) {
userOperation.User = undefined;
userOperation.Error = error;
callback(userOperation);
}
};
}
}
/**
* @wType System.ServiceModel.DomainServices.Client.ApplicationServices.WindowsAuthentication
*/
export class WindowsAuthentication extends WebAuthenticationService {}