"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.User=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),core_1=require("../../core"),iam_generated_1=require("./iam.generated"),policy_1=require("./policy"),principals_1=require("./principals"),util_1=require("./util");class User extends core_1.Resource{constructor(scope,id,props={}){super(scope,id,{physicalName:props.userName});this.grantPrincipal=this,this.principalAccount=this.env.account,this.assumeRoleAction="sts:AssumeRole",this.groups=new Array,this.managedPolicies=new Array,this.attachedPolicies=new util_1.AttachedPolicies,jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_UserProps(props),this.managedPolicies.push(...props.managedPolicies||[]),this.permissionsBoundary=props.permissionsBoundary;const user=new iam_generated_1.CfnUser(this,"Resource",{userName:this.physicalName,groups:util_1.undefinedIfEmpty(()=>this.groups),managedPolicyArns:core_1.Lazy.list({produce:()=>this.managedPolicies.map(p=>p.managedPolicyArn)},{omitEmpty:!0}),path:props.path,permissionsBoundary:this.permissionsBoundary?this.permissionsBoundary.managedPolicyArn:void 0,loginProfile:this.parseLoginProfile(props)});this.userName=this.getResourceNameAttribute(user.ref),this.userArn=this.getResourceArnAttribute(user.attrArn,{region:"",service:"iam",resource:"user",resourceName:this.physicalName}),this.policyFragment=new principals_1.ArnPrincipal(this.userArn).policyFragment,props.groups&&props.groups.forEach(g=>this.addToGroup(g))}static fromUserName(scope,id,userName){const userArn=core_1.Stack.of(scope).formatArn({service:"iam",region:"",resource:"user",resourceName:userName});return User.fromUserAttributes(scope,id,{userArn})}static fromUserArn(scope,id,userArn){return User.fromUserAttributes(scope,id,{userArn})}static fromUserAttributes(scope,id,attrs){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_UserAttributes(attrs);class Import extends core_1.Resource{constructor(){super(...arguments);this.grantPrincipal=this,this.principalAccount=core_1.Aws.ACCOUNT_ID,this.userName=core_1.Arn.extractResourceName(attrs.userArn,"user").split("/").pop(),this.userArn=attrs.userArn,this.assumeRoleAction="sts:AssumeRole",this.policyFragment=new principals_1.ArnPrincipal(attrs.userArn).policyFragment,this.attachedPolicies=new util_1.AttachedPolicies,this.groupId=0}addToPolicy(statement){return this.addToPrincipalPolicy(statement).statementAdded}addToPrincipalPolicy(statement){return this.defaultPolicy||(this.defaultPolicy=new policy_1.Policy(this,"Policy"),this.defaultPolicy.attachToUser(this)),this.defaultPolicy.addStatements(statement),{statementAdded:!0,policyDependable:this.defaultPolicy}}addToGroup(group){new iam_generated_1.CfnUserToGroupAddition(core_1.Stack.of(group),`${this.userName}Group${this.groupId}`,{groupName:group.groupName,users:[this.userName]}),this.groupId+=1}attachInlinePolicy(policy){this.attachedPolicies.attach(policy),policy.attachToUser(this)}addManagedPolicy(_policy){throw new Error("Cannot add managed policy to imported User")}}return new Import(scope,id)}addToGroup(group){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_IGroup(group),this.groups.push(group.groupName)}addManagedPolicy(policy){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_IManagedPolicy(policy),!this.managedPolicies.find(mp=>mp===policy)&&this.managedPolicies.push(policy)}attachInlinePolicy(policy){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_Policy(policy),this.attachedPolicies.attach(policy),policy.attachToUser(this)}addToPrincipalPolicy(statement){return jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_PolicyStatement(statement),this.defaultPolicy||(this.defaultPolicy=new policy_1.Policy(this,"DefaultPolicy"),this.defaultPolicy.attachToUser(this)),this.defaultPolicy.addStatements(statement),{statementAdded:!0,policyDependable:this.defaultPolicy}}addToPolicy(statement){return jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_PolicyStatement(statement),this.addToPrincipalPolicy(statement).statementAdded}parseLoginProfile(props){if(props.password)return{password:props.password.toString(),passwordResetRequired:props.passwordResetRequired};if(props.passwordResetRequired)throw new Error('Cannot set "passwordResetRequired" without specifying "initialPassword"')}}exports.User=User,_a=JSII_RTTI_SYMBOL_1,User[_a]={fqn:"aws-cdk-lib.aws_iam.User",version:"2.13.0"};
//# sourceMappingURL=user.js.map
