api enableStatus
This commit is contained in:
parent
f05c2f4b63
commit
d613637c95
2 changed files with 46 additions and 7 deletions
|
|
@ -29,6 +29,7 @@ import {
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
getRoleMappings,
|
getRoleMappings,
|
||||||
getUserCount,
|
getUserCount,
|
||||||
|
enableStatus,
|
||||||
} from "../keycloak";
|
} from "../keycloak";
|
||||||
import { AppDataSource } from "../database/data-source";
|
import { AppDataSource } from "../database/data-source";
|
||||||
import { Profile } from "../entities/Profile";
|
import { Profile } from "../entities/Profile";
|
||||||
|
|
@ -50,7 +51,6 @@ function stripLeadingSlash(str: string) {
|
||||||
@Tags("Single-Sign On")
|
@Tags("Single-Sign On")
|
||||||
@Security("bearerAuth")
|
@Security("bearerAuth")
|
||||||
export class KeycloakController extends Controller {
|
export class KeycloakController extends Controller {
|
||||||
|
|
||||||
private profileRepo = AppDataSource.getRepository(Profile);
|
private profileRepo = AppDataSource.getRepository(Profile);
|
||||||
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
|
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
|
||||||
|
|
||||||
|
|
@ -254,6 +254,7 @@ export class KeycloakController extends Controller {
|
||||||
lastname: x.lastName,
|
lastname: x.lastName,
|
||||||
email: x.email,
|
email: x.email,
|
||||||
roles: roles,
|
roles: roles,
|
||||||
|
enabled: x.enabled,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
@ -314,4 +315,10 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put("user/{userId}/enableStatus/{status}")
|
||||||
|
async changeEnableStatus(@Path() userId: string, @Path() status: boolean) {
|
||||||
|
const result = await enableStatus(userId, status);
|
||||||
|
if (!result) throw new Error("Failed. Cannot change enable status.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,7 @@ export async function getUserList(first = "", max = "", search = "") {
|
||||||
firstName: v.firstName,
|
firstName: v.firstName,
|
||||||
lastName: v.lastName,
|
lastName: v.lastName,
|
||||||
email: v.email,
|
email: v.email,
|
||||||
|
enabled: v.enabled,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,8 +234,6 @@ export async function updateName(
|
||||||
lastName,
|
lastName,
|
||||||
}),
|
}),
|
||||||
}).catch((e) => console.log("Keycloak Error: ", e));
|
}).catch((e) => console.log("Keycloak Error: ", e));
|
||||||
console.log("firstName: ", firstName);
|
|
||||||
console.log("lastName: ", lastName);
|
|
||||||
|
|
||||||
if (!res) return false;
|
if (!res) return false;
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|
@ -247,6 +246,39 @@ export async function updateName(
|
||||||
return id || true;
|
return id || true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enable keycloak user by uuid
|
||||||
|
*
|
||||||
|
* Client must have permission to manage realm's user
|
||||||
|
*
|
||||||
|
* @returns user uuid or true if success, false otherwise.
|
||||||
|
*/
|
||||||
|
export async function enableStatus(
|
||||||
|
userId: string,
|
||||||
|
status: boolean,
|
||||||
|
) {
|
||||||
|
|
||||||
|
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, {
|
||||||
|
headers: {
|
||||||
|
authorization: `Bearer ${await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
method: "PUT",
|
||||||
|
body: JSON.stringify({
|
||||||
|
enabled: status,
|
||||||
|
}),
|
||||||
|
}).catch((e) => console.log("Keycloak Error: ", e));
|
||||||
|
|
||||||
|
if (!res) return false;
|
||||||
|
if (!res.ok) {
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = res.headers.get("Location");
|
||||||
|
const id = path?.split("/").at(-1);
|
||||||
|
return id || true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete keycloak user by uuid
|
* Delete keycloak user by uuid
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue