/// <summary> /// Returns whether a Write operation can be performed by the active user on the current <see cref="EngineeringModel"/> contained /// <see cref="Thing"/> based on the supplied <see cref="Type"/>. The <see cref="Type"/> ultimately determines the access. /// </summary> /// <param name="thing">The <see cref="Thing"/> to write, or the container if the permissions are based /// on the container. /// </param> /// <param name="thingType">The <see cref="Type"/> that ultimately determines the permissions.</param> /// <returns>True if Write operation can be performed.</returns> private bool CanWriteEngineeringModelContainedThing(Thing thing, Type thingType) { var engineeringModel = thing.TopContainer; var iteration = thing is Iteration it ? it : thing.GetContainerOfType <Iteration>(); if (iteration?.IterationSetup.FrozenOn != null) { return(false); } var participant = this.Session.ActivePersonParticipants.FirstOrDefault(p => ((EngineeringModelSetup)p.Container).EngineeringModelIid == engineeringModel.Iid); if (participant?.Role == null) { return(false); } var permission = participant.Role.ParticipantPermission.SingleOrDefault(perm => perm.ObjectClass == thing.ClassKind); if (thing.GetType() != thingType) { if (Enum.TryParse(thingType.Name, out ClassKind superClassKind)) { permission = participant.Role.ParticipantPermission.SingleOrDefault(perm => perm.ObjectClass == superClassKind); } } // if the permission is not found then get the default one. var accessRightKind = permission?.AccessRight ?? StaticDefaultPermissionProvider.GetDefaultParticipantPermission(thingType.Name); switch (accessRightKind) { case ParticipantAccessRightKind.SAME_AS_CONTAINER: return(this.CanWrite(thing.Container, thing.Container.GetType())); case ParticipantAccessRightKind.SAME_AS_SUPERCLASS: return(this.CanWrite(thing, thing.GetType().BaseType)); case ParticipantAccessRightKind.MODIFY: return(true); case ParticipantAccessRightKind.MODIFY_IF_OWNER: if (thing is IOwnedThing ownedThing) { return(this.CanWriteIfParticipantOwned(ownedThing)); } break; default: return(false); } return(false); }
/// <summary> /// Returns whether a Write operation can be performed by the active user on the current <see cref="ClassKind"/> /// based on the supplied <see cref="Container"/>. The <see cref="ClassKind"/> ultimately determines the access. This method is primarily used for /// creation of a certain <see cref="SiteDirectory"/> contained <see cref="Thing"/>. /// </summary> /// <param name="classKind">The <see cref="ClassKind"/> that ultimately determines the permissions.</param> /// <param name="containerThing">The <see cref="Thing"/> to write to</param> /// <param name="thingType">The <see cref="ClassKind"/> that determine the permission</param> /// <returns>True if Write operation can be performed.</returns> private bool CanWriteSiteDirectoryContainedThing(ClassKind classKind, Thing containerThing, ClassKind thingType) { var person = this.Session.ActivePerson; if (person == null) { return(false); } var personRole = this.Session.ActivePerson.Role; if (personRole == null) { return(false); } var permission = personRole.PersonPermission.SingleOrDefault(p => p.ObjectClass == classKind); // if the permission is not found or superclass derivation is used then get the default one. var accessRightKind = permission?.AccessRight ?? StaticDefaultPermissionProvider.GetDefaultPersonPermission(thingType.ToString()); switch (accessRightKind) { case PersonAccessRightKind.SAME_AS_CONTAINER: return(this.CanWrite(containerThing, containerThing.GetType())); case PersonAccessRightKind.SAME_AS_SUPERCLASS: return(this.CanWriteBasedOnSuperclassClassKind(containerThing, thingType)); case PersonAccessRightKind.MODIFY: return(true); case PersonAccessRightKind.MODIFY_IF_PARTICIPANT: if (containerThing is EngineeringModelSetup setup) { return(setup.Participant.Any(x => x.Person == this.Session.ActivePerson)); } if (containerThing is SiteReferenceDataLibrary) { var rdl = this.Session.RetrieveSiteDirectory() .Model.SelectMany(ems => this.Session.GetEngineeringModelSetupRdlChain(ems)); return(rdl.Contains(containerThing)); } return(false); default: return(false); } }
/// <summary> /// Returns whether a Write operation can be performed by the active user on the current <see cref="ClassKind"/> /// based on the supplied <see cref="Container"/>. The <see cref="ClassKind"/> ultimately determines the access. This method is primarily used for /// creation of a certain <see cref="EngineeringModel"/> contained <see cref="Thing"/>. /// </summary> /// <param name="classKind">The <see cref="ClassKind"/> that ultimately determines the permissions.</param> /// <param name="containerThing">The <see cref="Thing"/> to write to</param> /// <param name="thingType">The <see cref="ClassKind"/> that ultimately determines the permissions.</param> /// <returns>True if Write operation can be performed.</returns> private bool CanWriteEngineeringModelContainedThing(ClassKind classKind, Thing containerThing, ClassKind thingType) { var engineeringModel = containerThing.TopContainer as EngineeringModel; var iteration = containerThing is Iteration it ? it : containerThing.GetContainerOfType <Iteration>(); if (iteration?.IterationSetup.FrozenOn != null) { return(false); } var participant = this.Session.ActivePersonParticipants.FirstOrDefault(p => ((EngineeringModelSetup)p.Container).EngineeringModelIid == engineeringModel.Iid); if (participant?.Role == null) { return(false); } var permission = participant.Role.ParticipantPermission.SingleOrDefault(perm => perm.ObjectClass == classKind); // if the permission is not found then get the default one. var right = permission?.AccessRight ?? StaticDefaultPermissionProvider.GetDefaultParticipantPermission(thingType.ToString()); switch (right) { case ParticipantAccessRightKind.SAME_AS_CONTAINER: return(this.CanWrite(containerThing, containerThing.GetType())); case ParticipantAccessRightKind.SAME_AS_SUPERCLASS: return(this.CanWriteBasedOnSuperclassClassKind(containerThing, thingType)); case ParticipantAccessRightKind.MODIFY: case ParticipantAccessRightKind.MODIFY_IF_OWNER: return(true); default: return(false); } }
/// <summary> /// Returns whether a Write operation can be performed by the active user on the current <see cref="SiteDirectory"/> contained /// <see cref="Thing"/> based on the supplied <see cref="Type"/>. The <see cref="Type"/> ultimately determines the access. /// </summary> /// <param name="thing">The <see cref="Thing"/> to write, or the container if the permissions are based /// on the container. /// </param> /// <param name="thingType">The <see cref="Type"/> that ultimately determines the permissions.</param> /// <returns>True if Write operation can be performed.</returns> private bool CanWriteSiteDirectoryContainedThing(Thing thing, Type thingType) { var person = this.Session.ActivePerson; if (person == null) { return(false); } var personRole = this.Session.ActivePerson.Role; if (personRole == null) { return(false); } var permission = personRole.PersonPermission.SingleOrDefault(p => p.ObjectClass == thing.ClassKind); if (thing.GetType() != thingType) { if (Enum.TryParse(thingType.Name, out ClassKind superClassKind)) { permission = personRole.PersonPermission.SingleOrDefault(perm => perm.ObjectClass == superClassKind); } } // if the permission is not found or superclass derivation is used then get the default one. var accessRightKind = permission?.AccessRight ?? StaticDefaultPermissionProvider.GetDefaultPersonPermission(thingType.Name); switch (accessRightKind) { case PersonAccessRightKind.SAME_AS_CONTAINER: return(this.CanWrite(thing.Container, thing.Container.GetType())); case PersonAccessRightKind.SAME_AS_SUPERCLASS: return(this.CanWrite(thing, thingType.BaseType)); case PersonAccessRightKind.MODIFY: return(true); case PersonAccessRightKind.MODIFY_IF_PARTICIPANT: if (thing is EngineeringModelSetup || thing.Container is EngineeringModelSetup) { var setup = thing as EngineeringModelSetup ?? thing.Container as EngineeringModelSetup; return(setup.Participant.Any(x => x.Person == this.Session.ActivePerson)); } if (thing is SiteReferenceDataLibrary) { var rdl = this.Session.RetrieveSiteDirectory() .Model.SelectMany(ems => this.Session.GetEngineeringModelSetupRdlChain(ems)); return(rdl.Contains(thing)); } return(false); case PersonAccessRightKind.MODIFY_OWN_PERSON: return(thing == person); default: return(false); } }