-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add run-user-template-monitoring command, clean up UserGroups a…
…nd minor fixes to user group monitoring files.
- Loading branch information
Showing
16 changed files
with
482 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/data/user-monitoring/user-template-monitoring/UserD2Repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Username } from "domain/entities/Base"; | ||
import { Async } from "domain/entities/Async"; | ||
import { User } from "domain/entities/user-monitoring/user-template-monitoring/Users"; | ||
import { UserRepository } from "domain/repositories/user-monitoring/user-template-monitoring/UserRepository"; | ||
import { D2Api, SelectedPick, D2UserSchema } from "@eyeseetea/d2-api/2.36"; | ||
|
||
export class UserD2Repository implements UserRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
async getByUsername(usernames: Username[]): Async<User[]> { | ||
const users = await this.api.models.users | ||
.get({ | ||
fields: userFields, | ||
filter: { username: { in: usernames } }, | ||
}) | ||
.getData(); | ||
|
||
return users.objects.map((user: D2User) => { | ||
return { | ||
...user, | ||
id: user.id, | ||
username: user.username, | ||
userCredentials: { | ||
...user.userCredentials, | ||
}, | ||
} as User; | ||
}); | ||
} | ||
} | ||
|
||
const userFields = { | ||
$all: true, | ||
username: true, | ||
userRoles: { id: true, name: true }, | ||
userGroups: { id: true, name: true }, | ||
userCredentials: { | ||
access: true, | ||
accountExpiry: true, | ||
attributeValues: true, | ||
catDimensionConstraints: true, | ||
code: true, | ||
cogsDimensionConstraints: true, | ||
created: true, | ||
createdBy: true, | ||
disabled: true, | ||
displayName: true, | ||
externalAccess: true, | ||
externalAuth: true, | ||
favorite: true, | ||
favorites: true, | ||
href: true, | ||
id: true, | ||
invitation: true, | ||
lastLogin: true, | ||
lastUpdated: true, | ||
lastUpdatedBy: true, | ||
ldapId: true, | ||
name: true, | ||
openId: true, | ||
password: true, | ||
passwordLastUpdated: true, | ||
publicAccess: true, | ||
selfRegistered: true, | ||
sharing: true, | ||
translations: true, | ||
twoFA: true, | ||
user: true, | ||
userAccesses: true, | ||
userGroupAccesses: true, | ||
userInfo: true, | ||
username: true, | ||
userRoles: false, | ||
}, | ||
} as const; | ||
|
||
type D2User = { | ||
username?: string; | ||
} & Partial<SelectedPick<D2UserSchema, typeof userFields>>; |
36 changes: 36 additions & 0 deletions
36
...ata/user-monitoring/user-template-monitoring/UserTemplatesMonitoringConfigD2Repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import _ from "lodash"; | ||
|
||
import log from "utils/log"; | ||
import { D2Api } from "@eyeseetea/d2-api/2.36"; | ||
import { Async } from "domain/entities/Async"; | ||
import { getObject } from "../common/GetDataStoreObjectByKey"; | ||
import { d2ToolsNamespace, Namespace } from "data/externalConfig/Namespaces"; | ||
import { UserTemplatesMonitoringOptions } from "domain/entities/user-monitoring/user-template-monitoring/UserTemplatesMonitoringOptions"; | ||
import { UserTemplatesMonitoringConfigRepository } from "domain/repositories/user-monitoring/user-template-monitoring/UserTemplatesMonitoringConfigRepository"; | ||
|
||
export class UserTemplatesMonitoringConfigD2Repository implements UserTemplatesMonitoringConfigRepository { | ||
private api: D2Api; | ||
|
||
constructor(api: D2Api) { | ||
this.api = api; | ||
} | ||
|
||
public async get(): Async<UserTemplatesMonitoringOptions> { | ||
const config = await getObject<UserTemplatesMonitoringOptions>( | ||
this.api, | ||
d2ToolsNamespace, | ||
Namespace.USER_TEMPLATE_MONITORING | ||
); | ||
|
||
if (!config) { | ||
log.warn("Error loading config from datastore"); | ||
throw new Error("Error loading config from datastore"); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
public async save(config: UserTemplatesMonitoringOptions): Promise<void> { | ||
await this.api.dataStore(d2ToolsNamespace).save(Namespace.USER_TEMPLATE_MONITORING, config).getData(); | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
src/domain/entities/user-monitoring/user-group-monitoring/UserGroups.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...omain/entities/user-monitoring/user-template-monitoring/UserTemplatesMonitoringOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Username } from "domain/entities/Base"; | ||
import { User } from "./Users"; | ||
|
||
export type UserTemplatesMonitoringOptions = { | ||
templatesToMonitor: Username[]; | ||
lastExecution: string; | ||
monitoredUserTemplates: User[]; | ||
}; |
26 changes: 26 additions & 0 deletions
26
src/domain/entities/user-monitoring/user-template-monitoring/Users.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { D2User, D2UserCredentials } from "@eyeseetea/d2-api/2.36"; | ||
import { Id, Username, NamedRef } from "domain/entities/Base"; | ||
|
||
type UserCredentials = Partial<Omit<D2UserCredentials, "userRoles">>; | ||
|
||
export type User = { | ||
id: Id; | ||
username: Username; | ||
userCredentials?: UserCredentials; | ||
} & Partial<D2User>; | ||
|
||
type MembershipChanges = { | ||
userRoles_Lost: NamedRef[]; | ||
userRoles_Added: NamedRef[]; | ||
userGroups_Lost: NamedRef[]; | ||
userGroups_Added: NamedRef[]; | ||
}; | ||
|
||
export type UserTemplateDiff = { | ||
id: Id; | ||
username: Username; | ||
newProps: Partial<User>; | ||
changedPropsLost: Partial<User>; | ||
changedPropsAdded: Partial<User>; | ||
membershipChanges: MembershipChanges; | ||
}; |
7 changes: 7 additions & 0 deletions
7
src/domain/repositories/user-monitoring/user-template-monitoring/UserRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Username } from "domain/entities/Base"; | ||
import { Async } from "domain/entities/Async"; | ||
import { User } from "domain/entities/user-monitoring/user-template-monitoring/Users"; | ||
|
||
export interface UserRepository { | ||
getByUsername(usernames: Username[]): Async<User[]>; | ||
} |
6 changes: 6 additions & 0 deletions
6
...ories/user-monitoring/user-template-monitoring/UserTemplatesMonitoringConfigRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { UserTemplatesMonitoringOptions } from "domain/entities/user-monitoring/user-template-monitoring/UserTemplatesMonitoringOptions"; | ||
|
||
export interface UserTemplatesMonitoringConfigRepository { | ||
get(): Promise<UserTemplatesMonitoringOptions>; | ||
save(config: UserTemplatesMonitoringOptions): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/domain/usecases/user-monitoring/user-template-monitoring/CompareUserTemplatesUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import _ from "lodash"; | ||
import { User, UserTemplateDiff } from "domain/entities/user-monitoring/user-template-monitoring/Users"; | ||
|
||
export class CompareUserTemplatesUseCase { | ||
constructor() {} | ||
|
||
private membershipKeys = ["userRoles", "userGroups"]; | ||
|
||
private getNewProps(oldUserTemplate: User, newUserTemplate: User): Partial<User> { | ||
let newProps: Partial<User> = {}; | ||
|
||
_.forOwn(newUserTemplate, (value, key) => { | ||
if (this.membershipKeys.includes(key)) { | ||
return; | ||
} | ||
|
||
if (!_.has(oldUserTemplate, key)) { | ||
newProps = _.set(newProps, key, value); | ||
} else { | ||
const oldValue = _.get(oldUserTemplate, key, undefined); | ||
if (!_.isEqual(value, oldValue)) { | ||
if ((_.isObjectLike(oldValue) || _.isArrayLike(oldValue)) && _.isEmpty(oldValue)) { | ||
newProps = _.set(newProps, key, value); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return newProps; | ||
} | ||
|
||
private compareMembershipProperties(oldUserTemplate: User, newUserTemplate: User): UserTemplateDiff { | ||
let changedPropsLost: Partial<User> = {}; | ||
let changedPropsAdded: Partial<User> = {}; | ||
let membershipChanges = { | ||
userRoles_Lost: [], | ||
userRoles_Added: [], | ||
userGroups_Lost: [], | ||
userGroups_Added: [], | ||
}; | ||
|
||
_.forOwn(oldUserTemplate, (value, key) => { | ||
if (_.has(newUserTemplate, key)) { | ||
const newValue: any = _.get(newUserTemplate, key, undefined); | ||
|
||
if (!_.isEqual(value, newValue)) { | ||
if (this.membershipKeys.includes(key)) { | ||
membershipChanges = _.set( | ||
membershipChanges, | ||
`${key}_Lost`, | ||
_.differenceBy(value as any, newValue, "id") | ||
); | ||
membershipChanges = _.set( | ||
membershipChanges, | ||
`${key}_Added`, | ||
_.differenceBy(newValue, value as any, "id") | ||
); | ||
} else { | ||
changedPropsLost = _.set(changedPropsLost, key, value); | ||
changedPropsAdded = _.set(changedPropsAdded, key, newValue); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const newProps = this.getNewProps(oldUserTemplate, newUserTemplate); | ||
|
||
return { | ||
id: oldUserTemplate.id, | ||
username: oldUserTemplate.username, | ||
changedPropsLost: changedPropsLost, | ||
changedPropsAdded: changedPropsAdded, | ||
membershipChanges: membershipChanges, | ||
newProps: newProps, | ||
}; | ||
} | ||
|
||
execute(oldUserTemplate: User, newUserTemplate: User): UserTemplateDiff { | ||
return this.compareMembershipProperties(oldUserTemplate, newUserTemplate); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...cases/user-monitoring/user-template-monitoring/GetUserTemplatesMonitoringConfigUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { UserTemplatesMonitoringConfigRepository } from "domain/repositories/user-monitoring/user-template-monitoring/UserTemplatesMonitoringConfigRepository"; | ||
|
||
export class GetUserTemplatesMonitoringConfigUseCase { | ||
constructor(private configRepository: UserTemplatesMonitoringConfigRepository) {} | ||
|
||
async execute() { | ||
return this.configRepository.get(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/domain/usecases/user-monitoring/user-template-monitoring/GetUserTemplatesUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Username } from "domain/entities/Base"; | ||
import { Async } from "domain/entities/Async"; | ||
import { User } from "domain/entities/user-monitoring/user-template-monitoring/Users"; | ||
import { UserRepository } from "domain/repositories/user-monitoring/user-template-monitoring/UserRepository"; | ||
|
||
export class GetUserTemplatesUseCase { | ||
constructor(private userGroupRepository: UserRepository) {} | ||
|
||
async execute(usernames: Username[]): Async<User[]> { | ||
return this.userGroupRepository.getByUsername(usernames); | ||
} | ||
} |
Oops, something went wrong.