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 { Settings } from '../models/settings';
22 import { saveInitialSettings, 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[]) {
41 export const logoutUser = () => (dispatcher: Dispatch, getState: () => IApplicationStoreState) =>{
43 const { framework:{ applicationState:{ authentication }, authenticationState: { user } } } = getState();
45 dispatcher(new UpdateUser(undefined));
46 dispatcher(new SetGeneralSettingsAction(null));
47 endWebsocketSession();
49 localStorage.removeItem('userToken');
52 //only call if a user is currently logged in
53 if (authentication === 'oauth' && user) {
55 const url = window.location.origin;
56 window.location.href = `${url}/oauth/logout`;
61 * Loads the user settings for the given user and dispatches a `saveInitialSettings` action with the result.
62 * @param user The user for which to load the settings.
63 * @param dispatcher The dispatcher function to use for dispatching the `saveInitialSettings` action.
65 const loadUserSettings = (user: User | undefined, dispatcher: Dispatch) => {
67 // 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
68 // no timeout used, because it's bad practice to add a timeout to hopefully avoid a race condition
69 // hence, fetch used to simply use supplied user data for getting settings
71 if (user && user.isValid) {
76 'Content-Type': 'application/json',
77 'Accept': 'application/json',
78 'Authorization': `${user.tokenType} ${user.token}`,
80 }).then((res: Response)=>{
81 if (res.status == 200) {
86 }).then((result:Settings)=>{
87 dispatcher(saveInitialSettings(result));
93 * Dispatches an `UpdateUser` action with the given user and starts a user session if the user is defined.
94 * Also loads the user settings for the given user and dispatches a `saveInitialSettings` action with the result.
95 * Finally, saves the user token to local storage.
96 * @param user The user to be logged in.
97 * @param dispatcher The dispatcher function to use for dispatching the actions.
99 export const loginUserAction = (user?: User) => (dispatcher: Dispatch) =>{
101 dispatcher(new UpdateUser(user));
103 startUserSession(user);
104 loadUserSettings(user, dispatcher);
105 localStorage.setItem('userToken', user.toString());