1
0
mirror of https://github.com/sasjs/core.git synced 2025-12-11 06:24:35 +00:00

Merge pull request #224 from sasjs/issue223

feat: ms_createuser macro
This commit is contained in:
Allan Bowe
2022-04-22 14:49:55 +03:00
committed by GitHub
8 changed files with 605 additions and 20 deletions

251
all.sas
View File

@@ -19023,6 +19023,150 @@ run;
options &optval;
%mend ms_createfile;
/**
@file
@brief Creates a user on SASjs Server
@details Creates a user on SASjs Server with the following attributes:
@li UserName
@li Password
@li isAdmin
@li displayName
The userid is created by sasjs/server. All users are created with `isActive`
set to `true`.
Example:
%ms_createuser(newuser,secretpass,displayname=New User!)
@param [in] username The username to apply. No spaces or special characters.
@param [in] password The initial password to set.
@param [in] isadmin= (false) Set to true to give the user admin rights
@param [in] displayName= (0) Set a friendly name (spaces & special characters
are ok). If not provided, username will be used instead.
@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
@param [out] outds= (work.ms_createuser) This output dataset will contain the
values from the JSON response (such as the id of the new user)
|ID:best.|DISPLAYNAME:$8.|USERNAME:$8.|ISACTIVE:best.|ISADMIN:best.|
|---|---|---|---|---|
|`6 `|`New User `|`newuser `|`1 `|`0 `|
<h4> SAS Macros </h4>
@li mf_getuniquefileref.sas
@li mf_getuniquelibref.sas
@li mp_abort.sas
<h4> Related Files </h4>
@li ms_createuser.test.sas
@li ms_getusers.sas
**/
%macro ms_createuser(username,password
,isadmin=false
,displayname=0
,outds=work.ms_createuser
,mdebug=0
);
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(syscc=&syscc on macro entry)
)
%local fref0 fref1 fref2 libref optval rc msg;
%let fref0=%mf_getuniquefileref();
%let fref1=%mf_getuniquefileref();
%let fref2=%mf_getuniquefileref();
%let libref=%mf_getuniquelibref();
/* avoid sending bom marker to API */
%let optval=%sysfunc(getoption(bomfile));
options nobomfile;
data _null_;
file &fref0 termstr=crlf;
username=quote(cats(symget('username')));
password=quote(cats(symget('password')));
isadmin=symget('isadmin');
displayname=quote(cats(symget('displayname')));
if displayname='"0"' then displayname=username;
%if &mdebug=1 %then %do;
putlog _all_;
%end;
put '{'@;
put '"displayName":' displayname @;
put ',"username":' username @;
put ',"password":' password @;
put ',"isAdmin":' isadmin @;
put ',"isActive": true }';
run;
data _null_;
file &fref1 lrecl=1000;
infile "&_sasjs_tokenfile" lrecl=1000;
input;
put "Authorization: Bearer " _infile_;
put "Content-Type: application/json";
put "accept: application/json";
run;
%if &mdebug=1 %then %do;
data _null_;
infile &fref0;
input;
put _infile_;
data _null_;
infile &fref1;
input;
put _infile_;
run;
%end;
proc http method='POST' in=&fref0 headerin=&fref1 out=&fref2
url="&_sasjs_apiserverurl/SASjsApi/user";
%if &mdebug=1 %then %do;
debug level=1;
%end;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(Issue submitting query to SASjsApi/user)
)
libname &libref JSON fileref=&fref2;
data &outds;
set &libref..root;
drop ordinal_root;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(Issue reading response JSON)
)
/* reset options */
options &optval;
%if &mdebug=1 %then %do;
filename &fref0 clear;
filename &fref1 clear;
filename &fref2 clear;
libname &libref clear;
%end;
%mend ms_createuser;
/**
@file
@brief Deletes a file from SASjs Drive
@@ -19121,6 +19265,113 @@ filename &binaryfref clear;
filename &headref clear;
%mend ms_getfile;/**
@file
@brief Fetches the list of users from SASjs Server
@details Fetches the list of users from SASjs Server and writes them to an
output dataset.
Example:
%ms_getusers(outds=userlist)
@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
@param [out] outds= (work.ms_getusers) This output dataset will contain the
list of user accounts. Format:
|DISPLAYNAME:$18.|USERNAME:$10.|ID:best.|
|---|---|---|
|`Super Admin `|`secretuser `|`1`|
|`Sabir Hassan`|`sabir`|`2`|
|`Mihajlo Medjedovic `|`mihajlo `|`3`|
|`Ivor Townsend `|`ivor `|`4`|
|`New User `|`newuser `|`5`|
<h4> SAS Macros </h4>
@li mf_getuniquefileref.sas
@li mf_getuniquelibref.sas
@li mp_abort.sas
<h4> Related Files </h4>
@li ms_createuser.sas
@li ms_getusers.test.sas
**/
%macro ms_getusers(
outds=work.ms_getusers
,mdebug=0
);
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(syscc=&syscc on macro entry)
)
%local fref0 fref1 libref optval rc msg;
%let fref0=%mf_getuniquefileref();
%let fref1=%mf_getuniquefileref();
%let libref=%mf_getuniquelibref();
/* avoid sending bom marker to API */
%let optval=%sysfunc(getoption(bomfile));
options nobomfile;
data _null_;
file &fref0 lrecl=1000;
infile "&_sasjs_tokenfile" lrecl=1000;
input;
put "Authorization: Bearer " _infile_;
put "accept: application/json";
run;
%if &mdebug=1 %then %do;
data _null_;
infile &fref0;
input;
put _infile_;
run;
%end;
proc http method='GET' headerin=&fref0 out=&fref1
url="&_sasjs_apiserverurl/SASjsApi/user";
%if &mdebug=1 %then %do;
debug level=1;
%end;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(Issue submitting GET query to SASjsApi/user)
)
libname &libref JSON fileref=&fref1;
data &outds;
set &libref..root;
drop ordinal_root;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(Issue reading response JSON)
)
/* reset options */
options &optval;
%if &mdebug=1 %then %do;
filename &fref0 clear;
filename &fref1 clear;
libname &libref clear;
%end;
%mend ms_getusers;
/**
@file
@brief Executes a SASjs Server Stored Program
@details Runs a Stored Program (using POST method) and extracts the webout and

View File

@@ -65,7 +65,7 @@
},
{
"name": "server",
"serverUrl": "https://sas.analytium.co.uk:5000",
"serverUrl": "https://sas.analytium.co.uk:5006",
"serverType": "SASJS",
"httpsAgentOptions": {
"allowInsecureRequests": false
@@ -74,13 +74,7 @@
"macroFolders": [
"server",
"tests/serveronly"
],
"programFolders": [],
"binaryFolders": [],
"deployConfig": {
"deployServicePack": true,
"deployScripts": []
}
]
},
{
"name": "docsonly",

144
server/ms_createuser.sas Normal file
View File

@@ -0,0 +1,144 @@
/**
@file
@brief Creates a user on SASjs Server
@details Creates a user on SASjs Server with the following attributes:
@li UserName
@li Password
@li isAdmin
@li displayName
The userid is created by sasjs/server. All users are created with `isActive`
set to `true`.
Example:
%ms_createuser(newuser,secretpass,displayname=New User!)
@param [in] username The username to apply. No spaces or special characters.
@param [in] password The initial password to set.
@param [in] isadmin= (false) Set to true to give the user admin rights
@param [in] displayName= (0) Set a friendly name (spaces & special characters
are ok). If not provided, username will be used instead.
@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
@param [out] outds= (work.ms_createuser) This output dataset will contain the
values from the JSON response (such as the id of the new user)
|ID:best.|DISPLAYNAME:$8.|USERNAME:$8.|ISACTIVE:best.|ISADMIN:best.|
|---|---|---|---|---|
|`6 `|`New User `|`newuser `|`1 `|`0 `|
<h4> SAS Macros </h4>
@li mf_getuniquefileref.sas
@li mf_getuniquelibref.sas
@li mp_abort.sas
<h4> Related Files </h4>
@li ms_createuser.test.sas
@li ms_getusers.sas
**/
%macro ms_createuser(username,password
,isadmin=false
,displayname=0
,outds=work.ms_createuser
,mdebug=0
);
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(syscc=&syscc on macro entry)
)
%local fref0 fref1 fref2 libref optval rc msg;
%let fref0=%mf_getuniquefileref();
%let fref1=%mf_getuniquefileref();
%let fref2=%mf_getuniquefileref();
%let libref=%mf_getuniquelibref();
/* avoid sending bom marker to API */
%let optval=%sysfunc(getoption(bomfile));
options nobomfile;
data _null_;
file &fref0 termstr=crlf;
username=quote(cats(symget('username')));
password=quote(cats(symget('password')));
isadmin=symget('isadmin');
displayname=quote(cats(symget('displayname')));
if displayname='"0"' then displayname=username;
%if &mdebug=1 %then %do;
putlog _all_;
%end;
put '{'@;
put '"displayName":' displayname @;
put ',"username":' username @;
put ',"password":' password @;
put ',"isAdmin":' isadmin @;
put ',"isActive": true }';
run;
data _null_;
file &fref1 lrecl=1000;
infile "&_sasjs_tokenfile" lrecl=1000;
input;
put "Authorization: Bearer " _infile_;
put "Content-Type: application/json";
put "accept: application/json";
run;
%if &mdebug=1 %then %do;
data _null_;
infile &fref0;
input;
put _infile_;
data _null_;
infile &fref1;
input;
put _infile_;
run;
%end;
proc http method='POST' in=&fref0 headerin=&fref1 out=&fref2
url="&_sasjs_apiserverurl/SASjsApi/user";
%if &mdebug=1 %then %do;
debug level=1;
%end;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(Issue submitting query to SASjsApi/user)
)
libname &libref JSON fileref=&fref2;
data &outds;
set &libref..root;
drop ordinal_root;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_createuser.sas
,msg=%str(Issue reading response JSON)
)
/* reset options */
options &optval;
%if &mdebug=1 %then %do;
filename &fref0 clear;
filename &fref1 clear;
filename &fref2 clear;
libname &libref clear;
%end;
%mend ms_createuser;

107
server/ms_getusers.sas Normal file
View File

@@ -0,0 +1,107 @@
/**
@file
@brief Fetches the list of users from SASjs Server
@details Fetches the list of users from SASjs Server and writes them to an
output dataset.
Example:
%ms_getusers(outds=userlist)
@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
@param [out] outds= (work.ms_getusers) This output dataset will contain the
list of user accounts. Format:
|DISPLAYNAME:$18.|USERNAME:$10.|ID:best.|
|---|---|---|
|`Super Admin `|`secretuser `|`1`|
|`Sabir Hassan`|`sabir`|`2`|
|`Mihajlo Medjedovic `|`mihajlo `|`3`|
|`Ivor Townsend `|`ivor `|`4`|
|`New User `|`newuser `|`5`|
<h4> SAS Macros </h4>
@li mf_getuniquefileref.sas
@li mf_getuniquelibref.sas
@li mp_abort.sas
<h4> Related Files </h4>
@li ms_createuser.sas
@li ms_getusers.test.sas
**/
%macro ms_getusers(
outds=work.ms_getusers
,mdebug=0
);
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(syscc=&syscc on macro entry)
)
%local fref0 fref1 libref optval rc msg;
%let fref0=%mf_getuniquefileref();
%let fref1=%mf_getuniquefileref();
%let libref=%mf_getuniquelibref();
/* avoid sending bom marker to API */
%let optval=%sysfunc(getoption(bomfile));
options nobomfile;
data _null_;
file &fref0 lrecl=1000;
infile "&_sasjs_tokenfile" lrecl=1000;
input;
put "Authorization: Bearer " _infile_;
put "accept: application/json";
run;
%if &mdebug=1 %then %do;
data _null_;
infile &fref0;
input;
put _infile_;
run;
%end;
proc http method='GET' headerin=&fref0 out=&fref1
url="&_sasjs_apiserverurl/SASjsApi/user";
%if &mdebug=1 %then %do;
debug level=1;
%end;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(Issue submitting GET query to SASjsApi/user)
)
libname &libref JSON fileref=&fref1;
data &outds;
set &libref..root;
drop ordinal_root;
run;
%mp_abort(
iftrue=(&syscc ne 0)
,mac=ms_getusers.sas
,msg=%str(Issue reading response JSON)
)
/* reset options */
options &optval;
%if &mdebug=1 %then %do;
filename &fref0 clear;
filename &fref1 clear;
libname &libref clear;
%end;
%mend ms_getusers;

View File

@@ -0,0 +1,50 @@
/**
@file
@brief Testing ms_createuser.sas macro
<h4> SAS Macros </h4>
@li mf_getuniquename.sas
@li mp_assert.sas
@li mp_assertscope.sas
@li ms_createuser.sas
@li ms_getusers.sas
**/
%let user=%substr(%mf_getuniquename(),1,8);
%mp_assertscope(SNAPSHOT)
%ms_createuser(&user,passwrd,outds=test1,mdebug=&sasjs_mdebug)
%mp_assertscope(COMPARE
,ignorelist=MCLIB0_JADP1LEN MCLIB0_JADPNUM MCLIB0_JADVLEN
)
%let id=0;
data _null_;
set work.test1;
call symputx('id',id);
run;
%mp_assert(
iftrue=(&id>0),
desc=Checking that user was created with an ID,
outds=work.test_results
)
/* double check by querying the list of users */
%ms_getusers(outds=work.test2)
%let checkid=0;
data _null_;
set work.test2;
where username="&user";
call symputx('checkid',id);
run;
%mp_assert(
iftrue=(&checkid=&id),
desc=Checking that fetched user exists and has the same ID,
outds=work.test_results
)

View File

@@ -0,0 +1,23 @@
/**
@file
@brief Testing ms_getusers.sas macro
<h4> SAS Macros </h4>
@li ms_getusers.sas
@li mp_assertdsobs.sas
@li mp_assertscope.sas
**/
%mp_assertscope(SNAPSHOT)
%ms_getusers(outds=work.test1,mdebug=&sasjs_mdebug)
%mp_assertscope(COMPARE
,ignorelist=MCLIB0_JADP1LEN MCLIB0_JADPNUM MCLIB0_JADVLEN
)
%mp_assertdsobs(work.test1,test=ATLEAST 1)

View File

@@ -3,14 +3,31 @@
@brief Testing ms_runstp.sas macro
<h4> SAS Macros </h4>
@li ms_runstp.sas
@li mf_getuniquename.sas
@li mp_assert.sas
@li mp_assertscope.sas
@li ms_createfile.sas
@li ms_runstp.sas
**/
/* first, create an STP to run */
filename stpcode temp;
data _null_;
file stpcode;
put '%put hello world;';
run;
options mprint;
%let fname=%mf_getuniquename();
%ms_createfile(/sasjs/tests/&fname..sas
,inref=stpcode
,mdebug=1
)
%mp_assertscope(SNAPSHOT)
%ms_runstp(/Public/app/frs/allan/services/common/appinit
%ms_runstp(/sasjs/tests/&fname
,debug=131
,outref=weboot
)
@@ -24,20 +41,16 @@ data _null_;
putlog _infile_;
run;
data work.httpheaders;
set webeen.httpheaders;
call symputx('test1',content_type);
run;
%let test1=0;
data work.log;
set webeen.log;
put (_all_)(=);
if _n_>10 then stop;
if _n_>10 then call symputx('test1',1);
run;
%mp_assert(
iftrue=("&test1"="application/json"),
desc=Checking line was created,
iftrue=("&test1"="1"),
desc=Checking log was returned,
outds=work.test_results
)

View File

@@ -14,12 +14,15 @@
/* set defaults */
%mp_init()
%global _debug;
%global _debug sasjs_mdebug;
%let sasjs_mdebug=0;
%macro loglevel();
%if "&_debug"="2477" or "&_debug"="fields,log,trace" %then %do;
%put debug mode activated;
options mprint mprintnest;
%let sasjs_mdebug=1;
%end;
%mend loglevel;