From 410cbc25acdc36f3eb3e6c2b406166c7960fa336 Mon Sep 17 00:00:00 2001 From: Ayush Goyal Date: Fri, 25 Sep 2020 03:42:40 +0000 Subject: [PATCH] fix(iam): User.fromUserName not implementing IUSER functions --- packages/@aws-cdk/aws-iam/lib/user.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-iam/lib/user.ts b/packages/@aws-cdk/aws-iam/lib/user.ts index 1491d2c454e70..9f5d391a88295 100644 --- a/packages/@aws-cdk/aws-iam/lib/user.ts +++ b/packages/@aws-cdk/aws-iam/lib/user.ts @@ -145,6 +145,9 @@ export class User extends Resource implements IIdentity, IUser { public readonly assumeRoleAction: string = 'sts:AssumeRole'; public readonly policyFragment: PrincipalPolicyFragment = new ArnPrincipal(arn).policyFragment; private defaultPolicy?: Policy; + private readonly groups = new Array(); + private readonly managedPolicies = new Array(); + private readonly attachedPolicies = new AttachedPolicies(); public addToPolicy(statement: PolicyStatement): boolean { return this.addToPrincipalPolicy(statement).statementAdded; @@ -159,16 +162,18 @@ export class User extends Resource implements IIdentity, IUser { return { statementAdded: true, policyDependable: this.defaultPolicy }; } - public addToGroup(_group: IGroup): void { - throw new Error('Cannot add imported User to Group'); + public addToGroup(group: IGroup): void { + this.groups.push(group.groupName); } - public attachInlinePolicy(_policy: Policy): void { - throw new Error('Cannot add inline policy to imported User'); + public attachInlinePolicy(policy: Policy): void { + this.attachedPolicies.attach(policy); + policy.attachToUser(this); } - public addManagedPolicy(_policy: IManagedPolicy): void { - throw new Error('Cannot add managed policy to imported User'); + public addManagedPolicy(policy: IManagedPolicy): void { + if (this.managedPolicies.find(mp => mp === policy)) { return; } + this.managedPolicies.push(policy); } }