protected override void ExecuteCmdlet() { base.ExecuteCmdlet(); Guid[] roleIds = Roles; Guid businessUnitId = BusinessUnit ?? SecurityManagementHelper.GetDefaultBusinessUnitId(_repository); Entity newUser = new Entity("systemuser") { Attributes = new AttributeCollection() }; newUser.Attributes.Add("domainname", UserName); newUser.Attributes.Add("firstname", Firstname); newUser.Attributes.Add("lastname", Lastname); newUser.Attributes.Add("accessmode", new OptionSetValue((int)Access)); newUser.Attributes.Add("caltype", new OptionSetValue((int)License)); newUser.Attributes.Add("businessunitid", new EntityReference("businessunit", businessUnitId)); Guid newUserId = _repository.Add(newUser); SecurityManagementHelper.LinkPrincipalRoles(_repository, "systemuser", newUserId, "role", roleIds); if (PassThru) { WriteObject(_repository.Get("systemuser", newUserId)); } }
protected override void ExecuteCmdlet() { base.ExecuteCmdlet(); string secondaryEntityName = null; switch (PrincipalType) { case CrmPrincipalType.User: secondaryEntityName = "systemuser"; break; case CrmPrincipalType.Team: secondaryEntityName = "team"; break; default: break; } string primaryEntityName = "role"; foreach (Guid roleId in Role) { Guid primaryEntityId = roleId; Guid[] secondaryEntityIds = Principals; Guid[] currentSetIds = SecurityManagementHelper.GetPrincipalsInRole(_repository, PrincipalType, roleId).Select(e => e.Id).ToArray(); Guid[] addSet = secondaryEntityIds.Except(currentSetIds).ToArray(); if (addSet != null && addSet.Length > 0) { SecurityManagementHelper.LinkPrincipalRoles(_repository, primaryEntityName, primaryEntityId, secondaryEntityName, addSet); } //Remove associations which are in current and not in new if (Overwrite) { Guid[] removeSet = currentSetIds.Except(secondaryEntityIds).ToArray(); if (removeSet != null && removeSet.Length > 0) { SecurityManagementHelper.UnlinkPrincipalRoles(_repository, primaryEntityName, primaryEntityId, secondaryEntityName, removeSet); } } if (PassThru) { WriteObject(_repository.Get(primaryEntityName, primaryEntityId)); } } }