Add seed code for sdnr app based on ONF Centennial
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / security / security-module / src / main / resources / security / src / security.service.ts
1 import * as angular from 'angularAMD';
2
3 const security = angular.module('app.security');
4
5 interface IEnvService {
6   getBaseURL(port: string): string;
7 }
8
9 export type User = {
10   description: string;
11   domainid: string;
12   email: string;
13   enabled: boolean;
14   password: string;
15   salt: string;
16   userid: string;
17 }
18
19 export type Role = {
20   roleid: string;
21   name: string;
22   description: string;
23   domainid: string;
24 }
25
26 export class SecurityService {
27   private credentials: ng.IPromise<string>;
28
29   constructor(private $q: ng.IQService, private $http: ng.IHttpService, private $window, private env: IEnvService) {
30     this.ensureCrendentials();
31   }
32
33   private ensureCrendentials() {
34     const credentialsDefer = this.$q.defer<string>();
35     this.credentials = credentialsDefer.promise;
36
37     const url = `${this.env.getBaseURL('MD_SAL')}/oauth2/token`;
38     this.$http<{ access_token: string }>({
39       method: "POST",
40       url: url,
41       headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
42       data: `grant_type=password&username=${this.$window.sessionStorage.odlUser}&password=${this.$window.sessionStorage.odlPass}&scope=sdn`
43     }).then(res => {
44       credentialsDefer.resolve(res.data && res.data.access_token);
45     }, err => {
46       credentialsDefer.reject(err);
47     });
48   }
49
50   public get token() {
51     return this.credentials;
52   }
53
54   public getAllUsers(): ng.IPromise<User[]> {
55     const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/users`;
56     return this.token.then(token => {
57       return this.$http<{ users: User[] }>({
58         method: "GET",
59         url: url,
60         headers: { 'Authorization': `Bearer ${token}` }
61       }).then(result => result.data && result.data.users)
62     });
63   }
64
65   public getAllRoles(): ng.IPromise<Role[]> {
66     const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/roles`;
67     return this.token.then(token => {
68       return this.$http<{ roles: Role[] }>({
69         method: "GET",
70         url: url,
71         headers: { 'Authorization': `Bearer ${token}` }
72       }).then(result => result.data && result.data.roles)
73     });
74   }
75
76   public getUserById(userId: string): ng.IPromise<User> {
77     const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/users/${userId}`;
78     return this.token.then(token => {
79       return this.$http<User>({
80         method: "GET",
81         url: url,
82         headers: { 'Authorization': `Bearer ${token}` }
83       }).then(result => result.data && result.data)
84     });
85   }
86
87   public getRolesForDomainUser(userId: string, domain: string= "sdn"): ng.IPromise<Role[]> {
88     const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/domains/${domain}/users/${userId}/roles`;
89     return this.token.then(token => {
90       return this.$http<{ roles: Role[] }>({
91         method: "GET",
92         url: url,
93         headers: { 'Authorization': `Bearer ${token}` }
94       }).then(result => result.data && result.data.roles)
95     });
96   }
97 }
98
99 security.service('securityService', ['$q', '$http', '$window', 'ENV', SecurityService]);