Skip to content

Commit

Permalink
[ADD] CanCreateOverride functionality to check for specific Create pe…
Browse files Browse the repository at this point in the history
…rmissions (#279)

* [ADD] CanCreateOverride functionality to check for specific Create permissions
* Undo check in CanWrite and beautify file
* Typo's
  • Loading branch information
lxatstariongroup authored Nov 8, 2023
1 parent c04920c commit a7a7640
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 56 deletions.
44 changes: 29 additions & 15 deletions CDP4Dal.NetCore.Tests/Permission/PermissionServiceTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PermissionServiceTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
// Copyright (c) 2015-2023 RHEA System S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft, Yevhen Ikonnykov
//
// This file is part of CDP4-SDK Community Edition
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-SDK Community Edition is free software; you can redistribute it and/or
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-SDK Community Edition is distributed in the hope that it will be useful,
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
Expand All @@ -27,13 +27,17 @@ namespace CDP4Dal.Tests.Permission
using System;
using System.Collections.Generic;
using System.Linq;

using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Common.Exceptions;
using CDP4Common.SiteDirectoryData;
using CDP4Dal.Permission;

using CDP4Dal.DAL;
using CDP4Dal.Permission;

using Moq;

using NUnit.Framework;

[TestFixture]
Expand Down Expand Up @@ -133,9 +137,10 @@ public void Setup()

this.session.Setup(x => x.ActivePerson).Returns(this.person);
this.session.Setup(x => x.Assembler).Returns(this.assembler);

this.session.Setup(x => x.OpenIterations).Returns(new Dictionary<Iteration, Tuple<DomainOfExpertise, Participant>>
{
{this.iteration, new Tuple<DomainOfExpertise,Participant>(this.domain1,this.participant)}
{ this.iteration, new Tuple<DomainOfExpertise, Participant>(this.domain1, this.participant) }
});

this.permissionService = new PermissionService(this.session.Object);
Expand All @@ -146,7 +151,6 @@ public void TearDown()
{
}

#region Person Permission
[Test]
public void TestCanWriteFalseWithDefaultPermission()
{
Expand Down Expand Up @@ -280,9 +284,6 @@ public void VerifyThatReadWriteIfParticipantWorks()
Assert.IsTrue(this.permissionService.CanRead(this.modelsetup));
Assert.IsTrue(this.permissionService.CanWrite(this.modelsetup));
}
#endregion

#region PArticipant Permission

[Test]
public void VerifyReadWriteParticipantPermission()
Expand All @@ -307,6 +308,7 @@ public void VerifyModifyIfOwnerForIterationsWithoutDomainOfExpertiseAndParticipa

var permission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.EngineeringModel);

var defpermission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.ElementDefinition);

Expand All @@ -321,7 +323,7 @@ public void VerifyModifyIfOwnerForIterationsWithoutDomainOfExpertiseAndParticipa

this.session.Setup(x => x.OpenIterations).Returns(new Dictionary<Iteration, Tuple<DomainOfExpertise, Participant>>
{
{this.iteration, new Tuple<DomainOfExpertise, Participant>(null,null)}
{ this.iteration, new Tuple<DomainOfExpertise, Participant>(null, null) }
});

Assert.IsFalse(this.permissionService.CanWrite(this.elementDef));
Expand All @@ -337,6 +339,7 @@ public void VerifyModifyIfOwnerForRequirement()

var permission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.Requirement);

var specPermission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.RequirementsSpecification);

Expand Down Expand Up @@ -371,7 +374,6 @@ public void VerifyModifyIfOwnerForRequirement()
this.requirementsSpecification.Owner = this.domain2;
Assert.IsFalse(this.permissionService.CanWrite(this.requirementsSpecification));
Assert.IsTrue(this.permissionService.CanRead(this.requirementsSpecification));

}

[Test]
Expand Down Expand Up @@ -401,7 +403,6 @@ public void VerifyModifyIfOwnerForThingsThatAreDirectlyUnderEngineeringModel()
Assert.IsTrue(this.permissionService.CanRead(this.commonFileStore));
}


[Test]
public void VerifySameAsSuperclassParticipantPermission()
{
Expand Down Expand Up @@ -429,7 +430,6 @@ public void VerifySameAsContainerParticipantPermission()
Assert.IsTrue(this.permissionService.CanWrite(this.valueset));
Assert.IsTrue(this.permissionService.CanRead(this.valueset));
}
#endregion

[Test]
public void VerifyCanWriteReturnsFalseWithFrozenIterationSetup()
Expand All @@ -451,5 +451,19 @@ public void VerifyCanWriteReturnsFalseWithFrozenIterationSetup()
Assert.IsFalse(this.permissionService.CanWrite(this.iteration));
Assert.IsFalse(this.permissionService.CanWrite(ClassKind.ElementDefinition, this.iteration));
}

[Test]
public void VerifyCanCreateOverrideReturnsExpectedResult()
{
this.session.Setup(x => x.ActivePersonParticipants).Returns(new List<Participant> { this.participant });

var permission = this.personRole.PersonPermission.Single(x => x.ObjectClass == ClassKind.EngineeringModelSetup);
permission.AccessRight = PersonAccessRightKind.MODIFY;
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.SiteDirectory), Is.False);

permission.AccessRight = PersonAccessRightKind.MODIFY_IF_PARTICIPANT;
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.SiteDirectory), Is.True);
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.EngineeringModelSetup), Is.False);
}
}
}
}
44 changes: 29 additions & 15 deletions CDP4Dal.Tests/Permission/PermissionServiceTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PermissionServiceTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
// Copyright (c) 2015-2023 RHEA System S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft, Yevhen Ikonnykov
//
// This file is part of CDP4-SDK Community Edition
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-SDK Community Edition is free software; you can redistribute it and/or
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-SDK Community Edition is distributed in the hope that it will be useful,
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
Expand All @@ -27,13 +27,17 @@ namespace CDP4Dal.Tests.Permission
using System;
using System.Collections.Generic;
using System.Linq;

using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Common.Exceptions;
using CDP4Common.SiteDirectoryData;
using CDP4Dal.Permission;

using CDP4Dal.DAL;
using CDP4Dal.Permission;

using Moq;

using NUnit.Framework;

[TestFixture]
Expand Down Expand Up @@ -133,9 +137,10 @@ public void Setup()

this.session.Setup(x => x.ActivePerson).Returns(this.person);
this.session.Setup(x => x.Assembler).Returns(this.assembler);

this.session.Setup(x => x.OpenIterations).Returns(new Dictionary<Iteration, Tuple<DomainOfExpertise, Participant>>
{
{this.iteration, new Tuple<DomainOfExpertise,Participant>(this.domain1,this.participant)}
{ this.iteration, new Tuple<DomainOfExpertise, Participant>(this.domain1, this.participant) }
});

this.permissionService = new PermissionService(this.session.Object);
Expand All @@ -146,7 +151,6 @@ public void TearDown()
{
}

#region Person Permission
[Test]
public void TestCanWriteFalseWithDefaultPermission()
{
Expand Down Expand Up @@ -280,9 +284,6 @@ public void VerifyThatReadWriteIfParticipantWorks()
Assert.IsTrue(this.permissionService.CanRead(this.modelsetup));
Assert.IsTrue(this.permissionService.CanWrite(this.modelsetup));
}
#endregion

#region PArticipant Permission

[Test]
public void VerifyReadWriteParticipantPermission()
Expand All @@ -307,6 +308,7 @@ public void VerifyModifyIfOwnerForIterationsWithoutDomainOfExpertiseAndParticipa

var permission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.EngineeringModel);

var defpermission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.ElementDefinition);

Expand All @@ -321,7 +323,7 @@ public void VerifyModifyIfOwnerForIterationsWithoutDomainOfExpertiseAndParticipa

this.session.Setup(x => x.OpenIterations).Returns(new Dictionary<Iteration, Tuple<DomainOfExpertise, Participant>>
{
{this.iteration, new Tuple<DomainOfExpertise, Participant>(null,null)}
{ this.iteration, new Tuple<DomainOfExpertise, Participant>(null, null) }
});

Assert.IsFalse(this.permissionService.CanWrite(this.elementDef));
Expand All @@ -337,6 +339,7 @@ public void VerifyModifyIfOwnerForRequirement()

var permission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.Requirement);

var specPermission =
this.participantRole.ParticipantPermission.Single(x => x.ObjectClass == ClassKind.RequirementsSpecification);

Expand Down Expand Up @@ -371,7 +374,6 @@ public void VerifyModifyIfOwnerForRequirement()
this.requirementsSpecification.Owner = this.domain2;
Assert.IsFalse(this.permissionService.CanWrite(this.requirementsSpecification));
Assert.IsTrue(this.permissionService.CanRead(this.requirementsSpecification));

}

[Test]
Expand Down Expand Up @@ -401,7 +403,6 @@ public void VerifyModifyIfOwnerForThingsThatAreDirectlyUnderEngineeringModel()
Assert.IsTrue(this.permissionService.CanRead(this.commonFileStore));
}


[Test]
public void VerifySameAsSuperclassParticipantPermission()
{
Expand Down Expand Up @@ -429,7 +430,6 @@ public void VerifySameAsContainerParticipantPermission()
Assert.IsTrue(this.permissionService.CanWrite(this.valueset));
Assert.IsTrue(this.permissionService.CanRead(this.valueset));
}
#endregion

[Test]
public void VerifyCanWriteReturnsFalseWithFrozenIterationSetup()
Expand All @@ -451,5 +451,19 @@ public void VerifyCanWriteReturnsFalseWithFrozenIterationSetup()
Assert.IsFalse(this.permissionService.CanWrite(this.iteration));
Assert.IsFalse(this.permissionService.CanWrite(ClassKind.ElementDefinition, this.iteration));
}

[Test]
public void VerifyCanCreateOverrideReturnsExpectedResult()
{
this.session.Setup(x => x.ActivePersonParticipants).Returns(new List<Participant> { this.participant });

var permission = this.personRole.PersonPermission.Single(x => x.ObjectClass == ClassKind.EngineeringModelSetup);
permission.AccessRight = PersonAccessRightKind.MODIFY;
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.SiteDirectory), Is.False);

permission.AccessRight = PersonAccessRightKind.MODIFY_IF_PARTICIPANT;
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.SiteDirectory), Is.True);
Assert.That(this.permissionService.CanCreateOverride(ClassKind.EngineeringModelSetup, ClassKind.EngineeringModelSetup), Is.False);
}
}
}
}
4 changes: 2 additions & 2 deletions CDP4Dal/CDP4Dal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net462;net47;net471;net472;net48;netstandard2.0;netstandard2.1</TargetFrameworks>
<Company>RHEA System S.A.</Company>
<Title>CDP4Dal Community Edition</Title>
<VersionPrefix>24.1.1</VersionPrefix>
<VersionPrefix>24.2.0</VersionPrefix>
<Description>CDP4 Data Access Layer library, a consumer of an ECSS-E-TM-10-25 Annex C API</Description>
<Copyright>Copyright © RHEA System S.A.</Copyright>
<Authors>Sam, Merlin, Alex, Naron, Alexander, Yevhen, Nathanael, Ahmed</Authors>
Expand All @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[UPGRADE] CDP4Common 24.1.1
[UPGRADE] CDP4Common 24.2.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
19 changes: 15 additions & 4 deletions CDP4Dal/Permission/IPermissionService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IPermissionService.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
// Copyright (c) 2015-2023 RHEA System S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou
//
// This file is part of CDP4-SDK Community Edition
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-SDK Community Edition is free software; you can redistribute it and/or
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-SDK Community Edition is distributed in the hope that it will be useful,
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
Expand Down Expand Up @@ -61,5 +61,16 @@ public interface IPermissionService
/// <param name="containerThing">The <see cref="Thing"/> to write to</param>
/// <returns>True if Write operation can be performed.</returns>
bool CanWrite(ClassKind classKind, Thing containerThing);

/// <summary>
/// Normally permission to Create, Update or Delete is handled by the CanWrite methods.
/// In some cases Create is allowed to be performed based on <see cref="PersonAccessRightKind"/>, but Update and Delete are not.
/// This method is an extra method that can be performed to check if Create is allowed, based on the TopContainer <see cref="ClassKind"/>
/// and the <see cref="ClassKind"/> of the <see cref="Thing"/> to create.
/// </summary>
/// <param name="classKind">The <see cref="ClassKind"/> that ultimately determines the permissions.</param>
/// <param name="containerClassKind">The <see cref="ClassKind"/> of the top container class where to create the classKind</param>
/// <returns>True if Create operation can be performed.</returns>
bool CanCreateOverride(ClassKind classKind, ClassKind containerClassKind);
}
}
Loading

0 comments on commit a7a7640

Please sign in to comment.