Ticket T1054371
Visible to All Users

Q359904: How to modify objects/properties in code when the user does not have the permission seems to not work.

created 3 years ago (modified 3 years ago)

Hello
I'm referring to the Q359904.
My question is concerning the behavior of the method directBaseUow_BeforeCommitTransaction of MySecuredObjectSpaceProvider when only the PayCard object is allowed (the PayCardUsage is not accessible).

Can you confirm that, despite we are operating within a non secured session, when you are saving an instance of the PayCard object then the result calculated by the CalculatePayMoneyAmount will not include the values of the PayCardUsage that are denied by the the TypePermission?
The above is what appears to be to me when we configure the above permissions, the PaycardUsages collection owned by the PayCard always contains 0 elements even from within the directBaseUow_BeforeCommitTransaction method.

BTW: I modified the original example just changing the permissions for the user role. Please execute the example, modify the current PayCard object and save. You will see that the property PayMoneyAmount will be zero instead of the sum of PayCardUsage objects. In this case is seems there is a Criteria "1==0" set on the PayCardUsages property of the Paycard object that do not allows to the get right collection.
Infact, querying directly the PayCardUsage records is possible (this confirm that we are within a non Secure session) but is seems to the that the Criteria 1==0 was created (and not removed) by the SecuritySystem. Do you confirm that this criteria created by the securitySystem? Any way to detect and remove it?

Can you suggest a way of keeping the suggested method (directBaseUow_BeforeCommitTransaction) but be sure that the PayMoneyAmount property of the PayCard object will for sure be updated with the right Sum regardless the permissions set on the PayCardUsage object?

Many thanks for your support.
D

Answers approved by DevExpress Support

created 3 years ago (modified 3 years ago)

Hello,

Indeed, in this usage scenario, you can't get values from the PayCard.PayCardUsages collection as XAF doesn't load it. To change this behavior, use one of the following ways:

C#
void directBaseUow_BeforeCommitTransaction(object sender, SessionManipulationEventArgs e) { List<PayCard> cardsToUpdate = new List<PayCard>(); foreach (System.Object item in e.Session.GetObjectsToSave()) { if (item is PayCard) { if (!cardsToUpdate.Contains((PayCard)item)){ cardsToUpdate.Add((PayCard)item); } } else if (item is PayCardUsage) { PayCardUsage payCardUsage = (PayCardUsage)item; if (payCardUsage.PayCard != null && !cardsToUpdate.Contains(payCardUsage.PayCard)) { cardsToUpdate.Add(payCardUsage.PayCard); } } } var lst = new XPCollection<PayCardUsage>(e.Session).ToList(); foreach (PayCard payCard in cardsToUpdate) { payCard.CalculatePayMoneyAmount(true); } }

Please note that we posted the solution from Q359904 (and the current one) as is and it may have limitations. Thoroughly test all use cases and scenarios. If you find some limitations, you will need to research and debug our source code to get the desired result.

Please also note that a simpler way is to create a non-secured IObjectSpace and work with secured data using it. You can see this solution at point 1 in Q359904 - SecurityStrategyComplex: How to modify objects/properties in code when the user does not have the permission?. Please consider using this solution instead of the BeforeCommitTransaction event.

Thanks,
Andrey

    Show previous comments (7)
    Andrey K (DevExpress Support) 3 years ago

      Hello,

      "The ({Not [PayMoney] = 1000.0m}) criteria is still on the payCard.PayCardUsages collection but now the collection is populated with records that do not match the original criteria… Is this expected? If yes, could you kindly explain me why?"

      Yes, this behavior is expected. When an app loads XPCollection<PayCardUsage>, it adds each PayCardUsage object to all related associated collections. You can refer to our source code to learn more about how exactly this happens.

      At the end: explicitly loading the collections (like you did with the "lst" variable) may leads in loosing not-saved objects so I think that I cannot use it.

      Would you please modify the example that you used before to demonstrate this behavior?

      According to my understanding, and please correct me if I'm wrong, there is not any possibility of doing a processing method (that may result also in not saving the changes) on the payCard.PayCardUsages collection in which we can take in consideration both some new PayCardUsage record that are created but not yet saved and any other PayCardUsage that are already in the database but are denied due to some kind of permissions. Is this consideration correct?

      I wasn't able to reproduce this behavior. Would you please modify your example so that it uses my code and shows the problematic behavior in action? It will help us better describe limitations of the solution that we suggested in Q359904.

      We look forward to your response.

      Thanks,
      Andrey

        Hello Andrey,
        Sorry for being late but I was out.
        Despite this was not clear to me, I think that this is also not reported in the documentation, your answer:
        "Yes, this behavior is expected. When an app loads XPCollection<PayCardUsage>, it adds each PayCardUsage object to all related associated collections. You can refer to our source code to learn more about how exactly this happens."
        clarified to me the overall behavior. For this reason I think that the newly loaded objects will be added to the collection without losing the not-saved objects.

        I have a last question: could you please tell me how to force "reloading" all related associated collections of an object without using an external call?
        I mean something like:

        C#
        payCard.Reload()

        Thanks in advance.
        D

        Andrey K (DevExpress Support) 3 years ago

          Hello,

          I created a separate ticket on your behalf: T1057333: How to reload all related associated collections. We placed it in our processing queue and will process it shortly.

          Thanks,
          Andrye

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.