in with the new
This commit is contained in:
78
src/core/plugins/auth/selectors.js
Normal file
78
src/core/plugins/auth/selectors.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { createSelector } from "reselect"
|
||||
import { List, Map } from "immutable"
|
||||
|
||||
const state = state => state
|
||||
|
||||
export const shownDefinitions = createSelector(
|
||||
state,
|
||||
auth => auth.get( "showDefinitions" )
|
||||
)
|
||||
|
||||
export const definitionsToAuthorize = createSelector(
|
||||
state,
|
||||
auth =>( { specSelectors } ) => {
|
||||
let definitions = specSelectors.securityDefinitions()
|
||||
let list = List()
|
||||
|
||||
//todo refactor
|
||||
definitions.entrySeq().forEach( ([ key, val ]) => {
|
||||
let map = Map()
|
||||
|
||||
map = map.set(key, val)
|
||||
list = list.push(map)
|
||||
})
|
||||
|
||||
return list
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
export const getDefinitionsByNames = ( state, securities ) =>( { specSelectors } ) => {
|
||||
let securityDefinitions = specSelectors.securityDefinitions()
|
||||
let result = List()
|
||||
|
||||
securities.valueSeq().forEach( (names) => {
|
||||
let map = Map()
|
||||
names.entrySeq().forEach( ([name, scopes]) => {
|
||||
let definition = securityDefinitions.get(name)
|
||||
let allowedScopes
|
||||
|
||||
if ( definition.get("type") === "oauth2" && scopes.size ) {
|
||||
allowedScopes = definition.get("scopes")
|
||||
|
||||
allowedScopes.keySeq().forEach( (key) => {
|
||||
if ( !scopes.contains(key) ) {
|
||||
allowedScopes = allowedScopes.delete(key)
|
||||
}
|
||||
})
|
||||
|
||||
definition = definition.set("allowedScopes", allowedScopes)
|
||||
}
|
||||
|
||||
map = map.set(name, definition)
|
||||
})
|
||||
|
||||
result = result.push(map)
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const authorized = createSelector(
|
||||
state,
|
||||
auth => auth.get("authorized") || Map()
|
||||
)
|
||||
|
||||
|
||||
export const isAuthorized = ( state, securities ) =>( { authSelectors } ) => {
|
||||
let authorized = authSelectors.authorized()
|
||||
let isAuth = false
|
||||
|
||||
return !!securities.toJS().filter( ( security ) => {
|
||||
let isAuthorized = true
|
||||
|
||||
return Object.keys(security).map((key) => {
|
||||
return !isAuthorized || !!authorized.get(key)
|
||||
}).indexOf(false) === -1
|
||||
}).length
|
||||
}
|
||||
Reference in New Issue
Block a user