Thursday, August 11, 2016

Check File path permission in AX 2012 x++

//Check File path permission in AX 2012 x++

Source:
http://ax2012anant.blogspot.com/2015/05/how-to-check-permission-access-of-ax.html

class --> AifFileSystemAdapter

client server static void validateExportfilePermissions(FilePath _filePath,
                                                        NoYesId  _checkRead,
                                                        NoYesId  _checkWrite,
                                                        NoYesId  _checkDelete)
{
     int requiredPermissions;
     int directoryPermissions;
     str errorMessage;
     Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem fileSystem;

     #AIF
     ;

     if (_checkRead)
     {
          requiredPermissions = AifDirectoryPermission::ReadFile;
     }
     if (_checkWrite)
     {
          requiredPermissions = requiredPermissions | AifDirectoryPermission::WriteFile;
     }
     if (_checkDelete)
     {
          requiredPermissions = requiredPermissions | AifDirectoryPermission::DeleteFile;
     }

     errorMessage = "@SYS95130";

     new InteropPermission(InteropKind::ClrInterop).assert();
     fileSystem = AifUtil::getClrObject(#FileSystemProgId);
     CodeAccessPermission::revertAssert();

     // Check that path exists
     new InteropPermission(InteropKind::ClrInterop).assert();
     if (!fileSystem.DoesDirectoryExist(_filePath))
          throw error(strfmt("@SYS95128", _filePath));
     CodeAccessPermission::revertAssert();

     // Check required directory permissions
     new InteropPermission(InteropKind::ClrInterop).assert();
     directoryPermissions = fileSystem.VerifyDirectoryPermissions(_filePath, requiredPermissions);
     CodeAccessPermission::revertAssert();

     if (requiredPermissions != directoryPermissions)
     {
          throw error(strfmt(errorMessage, _filePath));
     }
}

server static void serverTest(FilePath _filePath,
                              NoYesId  _checkRead,
                              NoYesId  _checkWrite,
                              NoYesId  _checkDelete)
{
     ;

     info("Server test start...");
     info(strFmt(" File path: %1", _filePath));
     info(strFmt(" User: %1", WinAPIServer::getUserName()));
     FileIOPermsTest::validateExportfilePermissions(_filePath, _checkRead, _checkWrite, _checkDelete);
     info("Server test passed");
}

client static void clientTest(FilePath _filePath,
                              NoYesId  _checkRead,
                              noYesId  _checkWrite,
                              NoYesId  _checkDelete)
{
     ;

     info("Client test start...");
     info(strFmt(" File path: %1", _filePath));
     info(strFmt(" User: %1", WINAPI::getUserName()));
     FileIOPermsTest::validateExportfilePermissions(_filePath, _checkRead, _checkWrite, _checkDelete);
     info("Client test passed");
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...