[SOLVED] ReadPermission in combination with RecRef

ta5ta5 Member Posts: 1,164
edited 2017-05-02 in NAV Three Tier
Hi

If have a strange issue with ReadPermission, NAV 2013R2.

This works
RecRef.OPEN(TableID);
If RecRef.READPERMISSION THEN…

This does not work (RunTimeError on first line of code, "Access is denied to Company <TheCompanyName>")
RecRef.OPEN(TableID,False,TheCompanyName);
If RecRef.READPERMISSION THEN…

So my problem is, code does not reach the line to test with ReadPermission, because the OPEN command fails.

Thanks in advance.
Thomas

Best Answer

Answers

  • da_nealda_neal Member Posts: 76
    May be your table has DataPerCompany = No?
  • NavNabNavNab Member Posts: 181
    Hello,

    "Access is denied to Company <TheCompanyName>" means simply that the user that runs the code does not have access to "TheCompanyName". Check the permissions for that user.

    This works RecRef.OPEN(TableID) => This code opens the RecRef in the current company. Your user have enough permissions in the current company

    This does not work RecRef.OPEN(TableID,False,TheCompanyName) => This code opens the RecRef in TheCompanyName. As already explained, your user does not have access to TheCompanyName

    @da_neal : DataPerCompany = No is not relevant for CHANGECOMPANY (or OPEN(TableID,False,TheCompanyName)) so it does not cause any runtime error.
  • ta5ta5 Member Posts: 1,164
    Hi
    @da_neal : Table is DataPerCompany=YES, so should not be the problem. But thank you anyway!

    @nabil.bamoh@hotmail.com: You describe exactly, what the problem is. My problem is to find out, whether the user has access to a given company, using ReadPermission. I know, I could check in Table Acess Control etc., but my intention was to use the function ReadPermission, which is actually a very handy function. The only problem is, I cannot check, because open command throws a runtime error before I can check. Do you have a good idea?

    Anybody else?
    Thank you
    Thomas


  • NavNabNavNab Member Posts: 181
    Hi @ta5 ,

    here is how you can achieve what you want "My problem is to find out, whether the user has access to a given company":

    code from standard NAV, page 9177 "Allowed Companies"

    Language.INIT;

    IF Company.FINDSET THEN
    REPEAT
    // Use a table that all users has access to and check if the user has permissions to open the company.
    IF Language.CHANGECOMPANY(Company.Name) THEN BEGIN
    Rec := Company;
    INSERT;
    END;
    UNTIL Company.NEXT = 0;

    You can combine this code with your previous code (inside the loop)
  • ta5ta5 Member Posts: 1,164
    Hi Nabil
    Thanks for answering. Good idea, I could take that as workaround.

    I'm not 100% happy, because of the following:

    1: I need to check for a third party tool, thats the reason I need RecRef, otherwise the Code won't compile if third party tool is not there. Not using RecRef would solve my problem instantly.
    2: User could have access to the company, but not for that very tool. in the company. For example the person could have access to Finance, but not to Fixed Assets. So checking for Language is not really fool proof.

    Maybe, there is still a better way?

    Thanks
    Thomas
  • NavNabNavNab Member Posts: 181
    Hi Thomas,

    I'm afraid you did not fully get the idea behind the table Language.

    The idea is to use a table that every user have read permission like "Language" and do a CHANGECOMPANY on this table to check access to a company... After that, you need to check RecRef.OPEN with READPERMISSION...

    Anyway, give a chance to the code below (of course you will need to adapt it to fit your specific need):

    9o0x8d2698om.jpg

    Best regards,
    Nabil
  • ta5ta5 Member Posts: 1,164
    Hi Nabil
    Thanks a lot, highly appreciated.
    It works as desired! At first sight I didn't see the clue....thanks again.
    Have a nice day.
    Thomas
Sign In or Register to comment.