${this.renderMappings(u)}
`
}).join('')}
`
this.#section.innerHTML = html
this.attachSectionEvents()
}
renderMappings(u){
const html = `
${this.#roles ? this.#roles.map(r=>{
const assigned = this.isAssigned(u, r)
return r.name !== 'uma_protection' ? `
-
${r.name}
` : ''
}).join('') : ''}
`
return html
}
isAssigned(u, r){
return u.mappings && u.mappings.filter(m=>m.id === r.id).length > 0
}
attachSectionEvents(){
this.#section.querySelector("ul[name=users]").addEventListener("click", ev=>{
const tgt = ev.target
if(tgt.getAttribute("name") === "role"){
const uid = tgt.getAttribute("data-userid")
const rid = tgt.getAttribute("data-roleid")
if(tgt.classList.contains("active")){
this.toggleMapping(uid, rid)
}else{
this.toggleMapping(uid, rid, true)
}
}
})
}
attachHeaderEvents(){
this.#header.querySelector("#search_user_filter").addEventListener("input", ev=>{
this.#userfilter = ev.target.value.toLowerCase()
this.renderUsers()
})
this.#header.querySelector("div.role-filter").addEventListener("change", ev=>{
this.#rolefilter = Array.prototype.slice.call(ev.currentTarget.querySelectorAll("input:checked")).map(r=>r.name)
this.renderUsers()
})
}
loadData(){
Promise.all([
this.loadEligibleUsers(), this.loadRoles()
]).then(()=>{
//console.log("Done. Ui should be complete now")
}).catch(err=>alert(err))
}
loadEligibleUsers(){
const url = this.#serviceurl + `/admin/realms/d4science/groups/${this.#groupid}/members?first=0&max=-1&briefRepresentation=true`
return this.#boot.secureFetch(url).then(resp=>{
if(resp.ok){
return resp.json()
}else if(resp.status === 403){
throw "Fetching users: You are not allowed to manage roles for this application."
}else{
throw "Fetching users: Unspecified error"
}
}).then(json=>{
this.#users = json
const prom = this.loadMappings()
this.renderUsers()
return prom
})
}
loadRoles(){
const url = this.#serviceurl + `/admin/realms/d4science/clients/${this.#appid}/roles`
return this.#boot.secureFetch(url).then(resp=>{
if(resp.ok){
return resp.json()
}else if(resp.status === 403){
throw "Fetching roles: You are not allowed to manage roles for this application."
}else{
throw "Fetching roles: Unspecified error"
}
}).then(json=>{
this.#roles = json
this.renderHeader()
})
}
loadMappings(){
return Promise.all(
this.#users.map(u => {
return this.loadMapping(u)
})
)
}
loadMapping(u){
const url = this.#serviceurl + `/admin/realms/d4science/users/${u.id}/role-mappings/clients/${this.#appid}`
return this.#boot.secureFetch(url).then(resp=>{
if(resp.ok){
return resp.json()
}else if(resp.status === 403){
throw "Fetching role mappings: You are not allowed to manage roles for this application."
}else{
throw "Fetching role mappings: Unspecified error"
}
}).then(json=>{
u.mappings = json
this.renderUsers()
})
}
toggleMapping(uid, rid, add){
const url = this.#serviceurl + `/admin/realms/d4science/users/${uid}/role-mappings/clients/${this.#appid}`
const role = this.#roles.filter(r=>r.id === rid)
if(role.length !== 1){
alert("Something went wrong with looking up the role")
return
}
this.#boot.secureFetch(url, { method : add ? "POST" : "DELETE", body : JSON.stringify(role), headers : { "Content-Type" : "application/json"}}).then(resp=>{
if(resp.ok){
return resp.text()
}else if(resp.status === 403){
throw "Assigning or removing role: You are not allowed to manage roles for this application."
}else{
throw "Assigning or removing role: Unspecified error"
}
}).then(()=>{
return this.loadMapping(this.#users.filter(u=>u.id === uid)[0])
}).catch(err=>alert(err))
}
}
window.customElements.define('d4s-extapp-role-manager', ExtAppRoleController);