M-script to determine if user has administrator rights

8 visualizaciones (últimos 30 días)
Jeremy
Jeremy el 15 de Ag. de 2016
Comentada: Walter Roberson el 3 de Nov. de 2019
I am seeing an issue where my script crashes for some users on some machines, because that user does not have administrative rights on that machine. Is there any way I can check in the m-script to see if the user has admin rights? I'm not asking if the user is running MatLab as an administrator, but if the user has the ability to run other programs (say a compiler which will be called by a build process later in the script) as an administrator.

Respuestas (3)

Guillaume
Guillaume el 15 de Ag. de 2016
Because access permission is such a complex beast on Windows, the best way to check if the user can do something is simply to attempt to do the something and check that it succeeded:
try
%do operation that may fail due to insufficient permissions
catch
%operation failed, handle any way you want, e.g.:
error('You do not have sufficient permissions to perform the operation. Log in as admin and try again.');
end
%rest of program ...
Note that being administrator may or may not be sufficient to do whatever you want to do and note that even non-administrators may be able to do it. If you really wanted to check before hand if the user can do your operation you would check for the exact permission required among the many possible permissions.
In any case, there is no tool in matlab to manage / check user permissions. If you really want to check permissions, on Windows, you would have to defer to .Net, for example the <https://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal(v=vs.110).aspx WindowsPrincipal class. I've not been able to work out how to use it reliably, unfortunately.
As said, try... catch is the best way to deal with this.

raym
raym el 18 de Mayo de 2019
function out = isadmin
out = System.Security.Principal.WindowsPrincipal(...
System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(...
System.Security.Principal.WindowsBuiltInRole.Administrator);
end
  1 comentario
Walter Roberson
Walter Roberson el 18 de Mayo de 2019
Interesting, but I would have thought that this is testing whether the user is using Run As Administrator, which the original poster indicates is not quite what they need

Iniciar sesión para comentar.


Syed Owais
Syed Owais el 3 de Nov. de 2019
hello
i am trying to add a function in matlab through edit windows but when i save it shows message that you dont have administrative rights to do so what should i do
  1 comentario
Walter Roberson
Walter Roberson el 3 de Nov. de 2019
You should rarely rarely save a function underneath the MATLAB installation directory.
About the only exception I have come across is that there is provision for a site-specific startup file that it would make sense for systems administrators to configure. However, in such a case the systems administrators would be able to grant themselves access to write the file.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by