2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import { Dispatch } from '../flux/store';
19 import { Action } from '../flux/action';
20 import { AuthPolicy, User } from '../models/authentication';
21 import { GeneralSettings, Settings } from '../models/settings';
22 import { saveInitialSettings, SetGeneralSettingsAction, setGeneralSettingsAction } from './settingsAction';
23 import { endWebsocketSession } from '../services/notificationService';
24 import { endUserSession, startUserSession } from '../services/userSessionService';
25 import { IApplicationStoreState } from '../store/applicationStore';
27 export class UpdateUser extends Action {
29 constructor (public user?: User) {
34 export class UpdatePolicies extends Action {
36 constructor (public authPolicies?: AuthPolicy[]) {
42 export const loginUserAction = (user?: User) => (dispatcher: Dispatch) =>{
44 dispatcher(new UpdateUser(user));
46 startUserSession(user);
47 loadUserSettings(user, dispatcher);
48 localStorage.setItem("userToken", user.toString());
52 export const logoutUser = () => (dispatcher: Dispatch, getState: () => IApplicationStoreState) =>{
54 const {framework:{applicationState:{ authentication }, authenticationState: {user}}} = getState();
56 dispatcher(new UpdateUser(undefined));
57 dispatcher(new SetGeneralSettingsAction(null));
58 endWebsocketSession();
60 localStorage.removeItem("userToken");
63 //only call if a user is currently logged in
64 if (authentication === "oauth" && user) {
66 const url = window.location.origin;
67 window.location.href=`${url}/oauth/logout`;
71 const loadUserSettings = (user: User | undefined, dispatcher: Dispatch) =>{
74 //fetch used, because state change for user login is not done when frameworks restRequest call is started (and is accordingly undefined -> /userdata call yields 401, unauthorized) and triggering an action from inside the handler / login event is impossible
75 //no timeout used, because it's bad practise to add a timeout to hopefully avoid a race condition
76 //hence, fetch used to simply use supplied user data for getting settings
78 if(user && user.isValid){
83 'Content-Type': 'application/json',
84 'Accept': 'application/json',
85 'Authorization': `${user.tokenType} ${user.token}`
87 }).then((res: Response)=>{
93 }).then((result:Settings)=>{
94 dispatcher(saveInitialSettings(result));