mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
384 lines
9.5 KiB
384 lines
9.5 KiB
4 years ago
|
// this.$axios.defaults.withCredentials = true;
|
||
|
|
||
3 years ago
|
// resourceState
|
||
|
import rolePermissionsCE from '@/helpers/rolePermissions'
|
||
|
import rolePermissionsEE from '@/helpers/rolePermissionsEE'
|
||
4 years ago
|
|
||
|
export const state = () => ({
|
||
|
|
||
|
// resourceList : []
|
||
|
user: null,
|
||
|
counter: 0,
|
||
|
social_auth_called: false,
|
||
|
paidUser: false,
|
||
|
ui_ability: {
|
||
|
rules: {
|
||
|
darkTheme: true,
|
||
|
maxTables: 99999,
|
||
|
maxTabs: 99,
|
||
|
disableGA: false,
|
||
3 years ago
|
disableTelemetry: false
|
||
4 years ago
|
}
|
||
|
},
|
||
|
token: null,
|
||
|
authType: null,
|
||
|
masterKey: null,
|
||
|
previewAs: null,
|
||
|
projectRole: {}
|
||
|
})
|
||
|
|
||
3 years ago
|
// modified state vars
|
||
4 years ago
|
export const getters = {
|
||
|
|
||
3 years ago
|
GtrCounter(state) {
|
||
3 years ago
|
return state.counter
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrUser(state) {
|
||
3 years ago
|
return state.user
|
||
4 years ago
|
},
|
||
3 years ago
|
GtrToken(state) {
|
||
3 years ago
|
return state.token
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrSocialAuthCalled(state) {
|
||
3 years ago
|
return state.social_auth_called
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrPaidUser(state) {
|
||
3 years ago
|
return state.paidUser
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrIsAuthenticated(state) {
|
||
|
return state.user
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrIsAdmin(state) {
|
||
4 years ago
|
// return (state.authType === 'jwt' && state.user && state.user.roles && (state.user.roles.creator || state.user.roles.owner)) || (state.authType === 'masterKey' && state.masterKey) || state.authType === 'none'
|
||
|
return (state.user && state.user.roles && (state.user.roles.creator || state.user.roles.owner)) ||
|
||
3 years ago
|
(state.projectRole && (state.projectRole.creator || state.projectRole.owner))
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
GtrIsUIAllowed(state) {
|
||
3 years ago
|
// eslint-disable-next-line no-unused-vars
|
||
3 years ago
|
const rolePermissions = process.env.EE ? rolePermissionsEE : rolePermissionsCE
|
||
3 years ago
|
return (page, ignorePreviewAs = false) => {
|
||
3 years ago
|
const user = state.user
|
||
4 years ago
|
let roles = {
|
||
3 years ago
|
...((user && user.roles) || {}),
|
||
|
...(state.projectRole || {})
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
if (state.previewAs && !ignorePreviewAs) {
|
||
4 years ago
|
roles = {
|
||
|
[state.previewAs]: true
|
||
|
}
|
||
|
}
|
||
3 years ago
|
return Object.entries(roles).some(([name, hasRole]) => {
|
||
3 years ago
|
// todo : revert
|
||
|
// return true
|
||
4 years ago
|
return hasRole && rolePermissions[name] && (rolePermissions[name] === '*' || rolePermissions[name][page])
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
GtrRole(state) {
|
||
3 years ago
|
return state.user && state.user.roles && (['owner',
|
||
4 years ago
|
'creator',
|
||
|
'editor',
|
||
3 years ago
|
'viewer'].find(r => state.user.roles[r]) || Object.keys(state.user.roles)[0])
|
||
3 years ago
|
},
|
||
|
|
||
|
GtrUserEmail(state) {
|
||
3 years ago
|
if (state.user && state.user.email) { return state.user.email } else { return '' }
|
||
|
}
|
||
4 years ago
|
|
||
|
}
|
||
|
|
||
|
// state variables are modified here
|
||
|
export const mutations = {
|
||
|
|
||
3 years ago
|
MutSetUser(state, user) {
|
||
3 years ago
|
// console.log('in MutPlusCounter', user);
|
||
4 years ago
|
|
||
3 years ago
|
state.user = user && user.email ? user : null
|
||
4 years ago
|
},
|
||
3 years ago
|
MutProjectRole(state, role) {
|
||
3 years ago
|
// console.log('in MutPlusCounter', user);
|
||
4 years ago
|
|
||
3 years ago
|
state.projectRole = role
|
||
4 years ago
|
},
|
||
3 years ago
|
MutSetToken(state, token) {
|
||
3 years ago
|
state.token = token
|
||
4 years ago
|
},
|
||
3 years ago
|
MutUiAbility(state, uiAbility) {
|
||
3 years ago
|
state.ui_ability = uiAbility
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutSetSocialAuthCalled(state, called) {
|
||
3 years ago
|
state.social_auth_called = called
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutSetProjectToNull(state) {
|
||
3 years ago
|
state.project = null
|
||
|
state.projectTree = null
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutSetPaidUser(state, isPaid) {
|
||
3 years ago
|
state.paidUser = isPaid
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutAuthType(state, authType) {
|
||
3 years ago
|
state.authType = authType
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
MutMasterKey(state, masterKey) {
|
||
3 years ago
|
state.masterKey = masterKey
|
||
4 years ago
|
},
|
||
3 years ago
|
MutPreviewAs(state, previewAs) {
|
||
3 years ago
|
state.previewAs = previewAs
|
||
|
}
|
||
4 years ago
|
|
||
|
}
|
||
|
|
||
|
// mutation are performed via actions
|
||
|
export const actions = {
|
||
|
|
||
3 years ago
|
ActPlusCounter({ commit }) {
|
||
3 years ago
|
// console.log('in action');
|
||
4 years ago
|
commit('MutPlusCounter')
|
||
|
},
|
||
|
|
||
3 years ago
|
ActPollSession({ commit, getters, rootGetters }, todo) {
|
||
|
setInterval(async() => {
|
||
4 years ago
|
if (getters.GtrUser) {
|
||
|
try {
|
||
3 years ago
|
const res = await this.$api.auth.me()
|
||
3 years ago
|
if (res === null || !res.email) {
|
||
4 years ago
|
commit('MutSetUser', null)
|
||
|
} else {
|
||
3 years ago
|
commit('MutSetUser', res)
|
||
4 years ago
|
}
|
||
3 years ago
|
commit('settings/MutPollingSet', 0, { root: true })
|
||
4 years ago
|
} catch (e) {
|
||
|
if (e.response.status === 504) {
|
||
3 years ago
|
commit('settings/MutPollingIncrementBy', 1, { root: true })
|
||
4 years ago
|
|
||
3 years ago
|
if (rootGetters['settings/GtrMaxPollingRetryExceeded']) {
|
||
3 years ago
|
commit('MutSetUser', null)
|
||
3 years ago
|
commit('settings/MutPollingSet', 0, { root: true })
|
||
4 years ago
|
}
|
||
|
} else {
|
||
3 years ago
|
commit('MutSetUser', null)
|
||
3 years ago
|
commit('settings/MutPollingSet', 0, { root: true })
|
||
4 years ago
|
}
|
||
|
}
|
||
|
} else {
|
||
|
}
|
||
3 years ago
|
}, process.env.pollingInterval)
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
async ActNewRefferalLink({ state, commit }) {
|
||
3 years ago
|
// console.log('ActNewRefferalLink');
|
||
4 years ago
|
try {
|
||
3 years ago
|
const profits = await this.$axios.post('/referral')
|
||
|
// console.log(profits);
|
||
|
return profits.data
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log('error', e)
|
||
|
return e
|
||
4 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
async ActGetUrl({ state, commit }, urlParamObj) {
|
||
3 years ago
|
// console.log('url:', urlParamObj['url']);
|
||
|
// console.log('params:', urlParamObj['params']);
|
||
4 years ago
|
try {
|
||
3 years ago
|
const value = await this.$axios.get(urlParamObj.url, { params: urlParamObj.params })
|
||
|
// console.log(value);
|
||
|
return value.data
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log('error in get url:', urlParamObj, e)
|
||
|
return e
|
||
4 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
async ActPostUrl({ state, commit }, urlParamObj) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
const value = await this.$axios.post(urlParamObj.url, urlParamObj.body)
|
||
|
// console.log(value);
|
||
|
return value.data
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log('error in post url:', e.response)
|
||
|
return e.response
|
||
4 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
/** ************** START : authentication ****************/
|
||
3 years ago
|
async ActSignUp({ commit, dispatch }, data) {
|
||
3 years ago
|
let err = null
|
||
4 years ago
|
try {
|
||
|
if (!data.ignore_subscribe) {
|
||
3 years ago
|
delete data.ignore_subscribe
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
|
const userRes = await this.$api.auth.signup(data)
|
||
|
|
||
|
commit('MutSetToken', userRes.token)
|
||
|
|
||
4 years ago
|
await dispatch('ActGetUserDetails')
|
||
|
} catch (e) {
|
||
3 years ago
|
console.log('ActSignUp error', e.response)
|
||
|
err = e.response
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
return err
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
async ActSignIn({ commit, dispatch }, data) {
|
||
3 years ago
|
// console.log('in action signin');
|
||
|
let err = null
|
||
4 years ago
|
try {
|
||
3 years ago
|
const userPromise = await this.$api.auth.signin(data)
|
||
|
|
||
|
commit('MutSetToken', userPromise.token)
|
||
|
|
||
|
await dispatch('ActGetUserDetails')
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
err = e.response
|
||
4 years ago
|
}
|
||
3 years ago
|
console.log('err', err)
|
||
|
return err
|
||
4 years ago
|
},
|
||
|
|
||
3 years ago
|
async ActSignOut({ commit, state }) {
|
||
3 years ago
|
let err = null
|
||
4 years ago
|
|
||
|
try {
|
||
3 years ago
|
// todo: sdk
|
||
3 years ago
|
commit('MutSetUser', null)
|
||
|
commit('MutSetToken', null)
|
||
|
commit('MutMasterKey', null)
|
||
|
commit('MutAuthType', null)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
err = e
|
||
|
console.log(e)
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
return err
|
||
4 years ago
|
},
|
||
3 years ago
|
/** ************** END : authentication ****************/
|
||
4 years ago
|
|
||
3 years ago
|
async ActGetUserDetails({ commit, state }) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
const user = await this.$api.auth.me({}, {
|
||
4 years ago
|
headers: {
|
||
|
'xc-auth': state.token
|
||
|
}
|
||
|
})
|
||
3 years ago
|
commit('MutSetUser', user)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log('ignoring user/me error')
|
||
4 years ago
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
async ActGetProjectUserDetails({ commit, state }, projectId) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
const user = await this.$api.auth.me({ project_id: projectId }, {
|
||
4 years ago
|
headers: {
|
||
|
'xc-auth': state.token
|
||
3 years ago
|
}
|
||
4 years ago
|
})
|
||
3 years ago
|
commit('MutProjectRole', user && user.roles)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log('ignoring user/me error')
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
async ActGetBaseUserDetails({ commit, state }, sharedBaseId) {
|
||
|
try {
|
||
|
try {
|
||
3 years ago
|
const user = await this.$api.auth.me({}, {
|
||
3 years ago
|
headers: {
|
||
|
'xc-shared-base-id': sharedBaseId
|
||
|
}
|
||
|
})
|
||
3 years ago
|
commit('MutProjectRole', user && user.roles)
|
||
3 years ago
|
} catch (e) {
|
||
|
console.log('ignoring user/me error')
|
||
|
}
|
||
|
} catch (e) {
|
||
|
console.log('ignoring user/me error')
|
||
|
}
|
||
|
},
|
||
4 years ago
|
|
||
3 years ago
|
async ActGetUserUiAbility({ commit, state }) {
|
||
4 years ago
|
try {
|
||
3 years ago
|
const uiAbility = await this.$axios.get('/ui_ability')
|
||
4 years ago
|
|
||
3 years ago
|
let changed = false
|
||
|
if (!state.ui_ability.rules.darkTheme && uiAbility.data.rules.darkTheme) {
|
||
4 years ago
|
this.$toast.success('Ho Yes! Dark theme has been enabled for your profile!').goAway(5000)
|
||
3 years ago
|
changed = true
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
commit('MutUiAbility', uiAbility.data)
|
||
4 years ago
|
|
||
3 years ago
|
if (changed) {
|
||
3 years ago
|
commit('settings/MutToggleDarkMode', true, { root: true })
|
||
3 years ago
|
}
|
||
4 years ago
|
} catch (e) {
|
||
|
console.log(e)
|
||
|
}
|
||
|
},
|
||
|
|
||
3 years ago
|
/** ************** START : social auth ****************/
|
||
4 years ago
|
|
||
3 years ago
|
async ActAuthGoogle({ commit, rootState }) {
|
||
3 years ago
|
// console.log('in action ActAuthGoogle', rootState);
|
||
4 years ago
|
|
||
|
try {
|
||
3 years ago
|
const url = '/auth/google'
|
||
|
// console.log(url);
|
||
4 years ago
|
|
||
3 years ago
|
await this.$axios.get(url
|
||
4 years ago
|
, JSON.stringify({
|
||
|
headers: {
|
||
|
crossDomain: true,
|
||
|
withCredentials: true,
|
||
|
credentials: 'same-origin'
|
||
|
}
|
||
|
})
|
||
3 years ago
|
)
|
||
4 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log(e)
|
||
4 years ago
|
}
|
||
3 years ago
|
}
|
||
4 years ago
|
|
||
3 years ago
|
/** ************** END : social auth ****************/
|
||
4 years ago
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
4 years ago
|
* @author Wing-Kam Wong <wingkwong.code@gmail.com>
|
||
4 years ago
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|