Files
redash/client/app/services/auth.js
Levko Kravets a682265e13 Migrate router and <app-view> to React (#4525)
* Migrate router and <app-view> to React: skeleton

* Update layout on route change

* Start moving page routes from angular to react

* Move page routes to react except of public dashboard and visualization embed)

* Move public dashboard and visualization embed routes to React

* Replace $route/$routeParams usages

* Some cleanup

* Replace AngularJS $location service with implementation based on history library

* Minor fix to how ApplicationView handles route change

* Explicitly use global layout for each page instead of handling related stuff in ApplicationArea component

* Error handling

* Remove AngularJS and related dependencies

* Move Parameter factory method to a separate file

* Fix CSS (replace custom components with classes)

* Fix: keep other url parts when updating location partially; refine code

* Fix tests

* Make router work in multi-org mode (respect <base> tag)

* Optimzation: don't resolve route if path didn't change

* Fix search input in header; error handling improvement (handle more errors in pages; global error handler for unhandled errors; dialog dismiss 'unhandled rejection' errors)

* Fix page keys; fix navigateTo calls (third parameter not available)

* Use relative links

* Router: ignore location REPLACE events, resolve only on PUSH/POP

* Fix tests

* Remove unused jQuery reference

* Show error from backend when creating Destination

* Remove route.resolve where not necessary (used constant values)

* New Query page: keep state on saving, reload when creating another new query

* Use currentRoute.key instead of hard-coded keys for page components

* Tidy up Router

* Tidy up location service

* Fix tests

* Don't add parameters changes to browser's history

* Fix test (improved fix)

Co-authored-by: Gabriel Dutra <nesk.frz@gmail.com>
2020-01-20 20:56:37 +02:00

101 lines
2.5 KiB
JavaScript

import debug from "debug";
import { includes, extend } from "lodash";
import location from "@/services/location";
import { axios } from "@/services/axios";
export const currentUser = {
canEdit(object) {
const userId = object.user_id || (object.user && object.user.id);
return this.hasPermission("admin") || (userId && userId === this.id);
},
canCreate() {
return (
this.hasPermission("create_query") || this.hasPermission("create_dashboard") || this.hasPermission("list_alerts")
);
},
hasPermission(permission) {
return includes(this.permissions, permission);
},
get isAdmin() {
return this.hasPermission("admin");
},
};
export const clientConfig = {};
export const messages = [];
const logger = debug("redash:auth");
const session = { loaded: false };
function updateSession(sessionData) {
logger("Updating session to be:", sessionData);
extend(session, sessionData, { loaded: true });
extend(currentUser, session.user);
extend(clientConfig, session.client_config);
extend(messages, session.messages);
}
export const Auth = {
isAuthenticated() {
return session.loaded && session.user.id;
},
login() {
const next = encodeURI(location.url);
logger("Calling login with next = %s", next);
window.location.href = `login?next=${next}`;
},
logout() {
logger("Logout.");
window.location.href = "logout";
},
loadSession() {
logger("Loading session");
if (session.loaded && session.user.id) {
logger("Resolving with local value.");
return Promise.resolve(session);
}
Auth.setApiKey(null);
return axios.get("api/session").then(data => {
updateSession(data);
return session;
});
},
loadConfig() {
logger("Loading config");
return axios.get("/api/config").then(data => {
updateSession({ client_config: data.client_config, user: { permissions: [] }, messages: [] });
return data;
});
},
setApiKey(apiKey) {
logger("Set API key to: %s", apiKey);
Auth.apiKey = apiKey;
},
getApiKey() {
return Auth.apiKey;
},
requireSession() {
logger("Requested authentication");
if (Auth.isAuthenticated()) {
return Promise.resolve(session);
}
return Auth.loadSession()
.then(() => {
if (Auth.isAuthenticated()) {
logger("Loaded session");
return session;
}
logger("Need to login, redirecting");
Auth.login();
})
.catch(() => {
logger("Need to login, redirecting");
Auth.login();
});
},
};