Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/XrmMockup365/Security.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ internal void AddPrinciplePrivileges(Guid principleId, Privileges privileges)
PrinciplePrivilages.Add(principleId, currentPrivileges);
}

private void AddPrinciplePrivlieges(Guid principleId, Guid[] roles)
private void AddPrinciplePrivileges(Guid principleId, Guid[] roles)
{
if (!roles.Any())
{
Expand All @@ -358,9 +358,14 @@ private void AddPrinciplePrivlieges(Guid principleId, Guid[] roles)
// Combine users current privilege with the securityRoles
foreach (var roleId in roles)
{
var securityRolePrivilages = SecurityRoles[roleId].Privileges;
if (!SecurityRoles.TryGetValue(roleId, out var securityRole))
{
throw new MockupException(
$"Security role with id '{roleId}' was not found. " +
$"Ensure the security role exists in the metadata or has been added via AddSecurityRole.");
}

currentPrivileges = JoinPrivileges(currentPrivileges, securityRolePrivilages);
currentPrivileges = JoinPrivileges(currentPrivileges, securityRole.Privileges);
}

// Update the principles privileges;
Expand All @@ -378,11 +383,7 @@ internal void SetSecurityRole(EntityReference entRef, Guid[] secRoles)
return;
}

AddPrinciplePrivlieges(entRef.Id, secRoles);
if (secRoles.Any(s => !SecurityRoles.ContainsKey(s)))
{
throw new MockupException($"Unknown security role");
}
AddPrinciplePrivileges(entRef.Id, secRoles);
var user = Core.GetDbRow(entRef).ToEntity();
var relationship = entRef.LogicalName == LogicalNames.SystemUser ? new Relationship("systemuserroles_association") : new Relationship("teamroles_association");
var roles = secRoles
Expand Down
11 changes: 11 additions & 0 deletions tests/XrmMockup365Test/TestSecurityRoles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ public void TestParentChangeCascading()
}
}

[Fact]
public void TestCreateUserWithNonExistentSecurityRoleThrowsMockupException()
{
var nonExistentRoleId = Guid.NewGuid();

var ex = Assert.Throws<MockupException>(() =>
crm.CreateUser(orgAdminUIService, crm.RootBusinessUnit, nonExistentRoleId));

Assert.Contains(nonExistentRoleId.ToString(), ex.Message);
Assert.Contains("was not found", ex.Message);
}
}

}
Loading