Skip to content

Commit

Permalink
feat(admin-ui): provide roles and permissions management screens in A…
Browse files Browse the repository at this point in the history
…dmin UI #339
  • Loading branch information
syntrydy committed Jan 9, 2022
2 parents 957dc49 + 549951a commit 98e6318
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 64 deletions.
3 changes: 2 additions & 1 deletion app/redux/actions/AuthActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
GET_USER_LOCATION,
} from './types'

export const getOAuth2Config = () => ({
export const getOAuth2Config = (token) => ({
type: GET_OAUTH2_CONFIG,
payload: { token },
})

export const getOAuth2ConfigResponse = (config) => ({
Expand Down
7 changes: 4 additions & 3 deletions app/redux/actions/LicenseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import {
ACTIVATE_LICENSE_RESPONSE,
} from './types'

export const checkLicensePresent = () => ({
export const checkLicensePresent = (token) => ({
type: CHECK_FOR_VALID_LICENSE,
payload: { token },
})

export const checkLicensePresentResponse = (isLicenseValid) => ({
type: CHECK_FOR_VALID_LICENSE_RESPONSE,
payload: { isLicenseValid },
})

export const activateLicense = (licenseKey) => ({
export const activateLicense = (licenseKey, token) => ({
type: ACTIVATE_LICENSE,
payload: { licenseKey },
payload: { licenseKey, token },
})

export const activateLicenseResponse = (isLicenseValid) => ({
Expand Down
26 changes: 21 additions & 5 deletions app/redux/api/backend-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import axios from '../api/axios'
import axios_instance from 'axios'

// Get OAuth2 Configuration
export const fetchServerConfiguration = async () => {
export const fetchServerConfiguration = async (token) => {
const headers = { Authorization: `Bearer ${token}`};
return axios
.get('/oauth2/config')
.get('/oauth2/config', {headers})
.then((response) => response.data)
.catch((error) => {
console.error(
Expand Down Expand Up @@ -71,10 +72,24 @@ export const fetchApiAccessToken = async (jwt) => {
})
}

export const fetchApiTokenWithDefaultScopes = async () => {
return axios
.get('/oauth2/api-protection-token')
.then((response) => response.data)
.catch((error) => {
console.error(
'Problems getting API access token in order to process api calls.',
error,
)
return -1
})
}

// Check License present
export const checkLicensePresent = async () => {
export const checkLicensePresent = async (token) => {
const headers = { Authorization: `Bearer ${token}`};
return axios
.get('/license/checkLicense')
.get('/license/checkLicense', {headers})
.then((response) => response.data)
.catch((error) => {
console.error('Error checking license of admin-ui', error)
Expand All @@ -83,12 +98,13 @@ export const checkLicensePresent = async () => {
}

// Activate license using key
export const activateLicense = async (licenseKey) => {
export const activateLicense = async (licenseKey, token) => {
let data = { licenseKey: licenseKey }
return axios
.post('/license/activateLicense', data, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
})
.then((response) => response.data)
Expand Down
9 changes: 8 additions & 1 deletion app/redux/sagas/AuthSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ import {
fetchUserInformation,
fetchApiAccessToken,
getUserIpAndLocation,
fetchApiTokenWithDefaultScopes,
} from '../api/backend-api'

function* getApiTokenWithDefaultScopes() {
const response = yield call(fetchApiTokenWithDefaultScopes)
return response.access_token
}

function* getOAuth2ConfigWorker() {
try {
const response = yield call(fetchServerConfiguration)
const token = yield* getApiTokenWithDefaultScopes();
const response = yield call(fetchServerConfiguration, token)
if (response) {
yield put(getOAuth2ConfigResponse(response))
return
Expand Down
13 changes: 10 additions & 3 deletions app/redux/sagas/LicenseSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import {
activateLicenseResponse,
} from '../actions'

import { checkLicensePresent, activateLicense } from '../api/backend-api'
import { checkLicensePresent, activateLicense, fetchApiTokenWithDefaultScopes } from '../api/backend-api'

function* getApiTokenWithDefaultScopes() {
const response = yield call(fetchApiTokenWithDefaultScopes)
return response.access_token
}

function* checkLicensePresentWorker() {
try {
const response = yield call(checkLicensePresent)
const token = yield* getApiTokenWithDefaultScopes();
const response = yield call(checkLicensePresent, token)
if (response) {
yield put(checkLicensePresentResponse(response))
return
Expand All @@ -25,7 +31,8 @@ function* checkLicensePresentWorker() {

function* activateLicenseWorker({ payload }) {
try {
const response = yield call(activateLicense, payload.licenseKey)
const token = yield* getApiTokenWithDefaultScopes();
const response = yield call(activateLicense, payload.licenseKey, token)
if (response) {
yield put(activateLicenseResponse(response))
return
Expand Down
1 change: 1 addition & 0 deletions app/routes/Apps/Gluu/Tests/GluuFormDetailRow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ it('Should render one label and a badge', () => {
)
screen.getByText('Application Type:')
screen.getByText(VALUE)
expect(screen.getByDisplayValue(VALUE).id).toBe(NAME)
})
4 changes: 4 additions & 0 deletions app/utils/PermChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export const PERMISSION_WRITE =
export const PERMISSION_DELETE =
'https://jans.io/oauth/jans-auth-server/config/adminui/user/permission.delete'

export const LICENSE_DETAILS_READ = BASE_URL + '/config/adminui/license.readonly'
export const LICENSE_DETAILS_WRITE = BASE_URL + '/config/adminui/license.write'


export const SCOPE_READ = BASE_URL + '/config/scopes.readonly'
export const SCOPE_WRITE = BASE_URL + '/config/scopes.write'
export const SCOPE_DELETE = BASE_URL + '/config/scopes.delete'
Expand Down
1 change: 1 addition & 0 deletions plugins/admin/common/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export const FETCHING_SCOPES = 'Fetch scopes'
export const SEARCHING_SCRIPTS = 'Search scripts'
export const FETCHING_SCRIPTS = 'Fetch scripts'

export const FETCHING_LICENSE_DETAILS = 'Fetch license details'


18 changes: 15 additions & 3 deletions plugins/admin/components/Configuration/LicenseDetailsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ import {
Row,
Col,
} from '../../../../app/components'
import {
buildPayload,
} from '../../../../app/utils/PermChecker'
import GluuLoader from '../../../../app/routes/Apps/Gluu/GluuLoader'
import Alert from '@material-ui/lab/Alert'
import {
FETCHING_LICENSE_DETAILS,
} from '../../common/Constants'

function LicenseDetailsPage({ item, loading, dispatch }) {
const userAction = {}
const options = {}

useEffect(() => {
dispatch(getLicenseDetails())
buildPayload(userAction, FETCHING_LICENSE_DETAILS, options)
dispatch(getLicenseDetails({}))
}, [])


function handleSubmit(data) {
if (data) {
dispatch(updateLicenseDetails(data))
buildPayload(userAction, 'message', data)
dispatch(updateLicenseDetails(userAction))
}
}

Expand All @@ -41,7 +53,7 @@ function LicenseDetailsPage({ item, loading, dispatch }) {
<FormGroup row />
</CardTitle>
<GluuLoader blocking={loading}>
{item.licenseEnable ? (
{item.licenseEnabled ? (
<>
<Container style={{ backgroundColor: '#F5F5F5' }}>
<Row>
Expand Down
7 changes: 4 additions & 3 deletions plugins/admin/redux/actions/LicenseDetailsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import {
UPDATE_LICENSE_DETAILS_RESPONSE,
} from './types'

export const getLicenseDetails = () => ({
export const getLicenseDetails = (action) => ({
type: GET_LICENSE_DETAILS,
payload: { action },
})

export const getLicenseDetailsResponse = (data) => ({
type: GET_LICENSE_DETAILS_RESPONSE,
payload: { data },
})

export const updateLicenseDetails = (data) => ({
export const updateLicenseDetails = (action) => ({
type: UPDATE_LICENSE_DETAILS,
payload: { data },
payload: { action },
})

export const updateLicenseDetailsResponse = (data) => ({
Expand Down
51 changes: 28 additions & 23 deletions plugins/admin/redux/api/LicenseDetailsApi.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import axios from '../../../../app/redux/api/axios'
export default class LicenseDetailsApi {
constructor(api) {
this.api = api
}

// Get License Details
export const getLicenseDetails = async () => {
return axios
.get('/license/getLicenseDetails')
.then((response) => response.data)
.catch((error) => {
console.error('Problems getting license details.', error)
return -1
getLicenseDetails = () => {
return new Promise((resolve, reject) => {
this.api.getAdminuiLicense((error, data) => {
this.handleResponse(error, reject, resolve, data)
})
})
}
//update License Details
export const updateLicenseDetails = async (licenseDetails) => {
return axios
.put('/license/updateLicenseDetails', licenseDetails, {
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => response)
.catch((error) => {
console.error('Problems updating license details.', error)
return -1
}

updateLicenseDetails = (data) => {
const options = {}
options['licenseRequest'] = data
return new Promise((resolve, reject) => {
this.api.editAdminuiLicense(options, (error, options) => {
this.handleResponse(error, reject, resolve, data)
})
})
}
}

handleResponse(error, reject, resolve, data) {
if (error) {
reject(error)
} else {
resolve(data)
}
}
}
68 changes: 46 additions & 22 deletions plugins/admin/redux/sagas/LicenseDetailsSaga.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
import { all, call, fork, put, takeEvery } from 'redux-saga/effects'
import { all, call, fork, put, takeEvery, select } from 'redux-saga/effects'
import { GET_LICENSE_DETAILS, UPDATE_LICENSE_DETAILS } from '../actions/types'
import {
getLicenseDetailsResponse,
updateLicenseDetailsResponse,
} from '../actions/LicenseDetailsActions'

import { getClient } from '../../../../app/redux/api/base'
import LicenseDetailsApi from '../api/LicenseDetailsApi'
const JansConfigApi = require('jans_config_api')
import { initAudit } from '../../../../app/redux/sagas/SagaUtils'
import {
getLicenseDetails,
updateLicenseDetails,
} from '../api/LicenseDetailsApi'
isFourZeroOneError,
addAdditionalData,
} from '../../../../app/utils/TokenController'
import { getAPIAccessToken } from '../../../../app/redux/actions/AuthActions'

function* newFunction() {
const token = yield select((state) => state.authReducer.token.access_token)
const issuer = yield select((state) => state.authReducer.issuer)
const api = new JansConfigApi.AdminUILicenseApi(
getClient(JansConfigApi, token, issuer),
)
return new LicenseDetailsApi(api)
}


function* getLicenseDetailsWorker() {
export function* getLicenseDetailsWorker({ payload }) {
const audit = yield* initAudit()
try {
const response = yield call(getLicenseDetails)
if (response) {
yield put(getLicenseDetailsResponse(response))
return
//addAdditionalData(audit, FETCH, GET_LICENSE_DETAILS, payload)
const licenseApi = yield* newFunction()
const data = yield call(licenseApi.getLicenseDetails)
yield put(getLicenseDetailsResponse(data))
yield call(postUserAction, audit)
} catch (e) {
yield put(getLicenseDetailsResponse(null))
if (isFourZeroOneError(e)) {
const jwt = yield select((state) => state.authReducer.userinfo_jwt)
yield put(getAPIAccessToken(jwt))
}
} catch (error) {
console.log('Error in fetching License Details.', error)
}
yield put(getLicenseDetailsResponse())
}

function* updateLicenseDetailsWorker({ payload }) {
export function* updateLicenseDetailsWorker({ payload }) {
const audit = yield* initAudit()
try {
const response = yield call(updateLicenseDetails, payload.data)
if (response) {
yield put(updateLicenseDetailsResponse(response))
return
//addAdditionalData(audit, UPDATE, UPDATE_LICENSE_DETAILS, payload)
const roleApi = yield* newFunction()
const data = yield call(roleApi.updateLicenseDetails, payload.action.action_data)
yield put(updateLicenseDetailsResponse(data))
yield call(postUserAction, audit)
} catch (e) {
yield put(updateLicenseDetailsResponse(null))
if (isFourZeroOneError(e)) {
const jwt = yield select((state) => state.authReducer.userinfo_jwt)
yield put(getAPIAccessToken(jwt))
}
} catch (error) {
console.log('Error in fetching License Details.', error)
}
yield put(updateLicenseDetailsResponse())
}


export function* getLicenseWatcher() {

yield takeEvery(GET_LICENSE_DETAILS, getLicenseDetailsWorker)
}

Expand All @@ -46,4 +70,4 @@ export function* updateLicenseWatcher() {

export default function* rootSaga() {
yield all([fork(getLicenseWatcher), fork(updateLicenseWatcher)])
}
}

0 comments on commit 98e6318

Please sign in to comment.