Macros for SAS Application Developers
https://github.com/sasjs/core
ms_createuser.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a user on SASjs Server
4  @details Creates a user on SASjs Server with the following attributes:
5 
6  @li UserName
7  @li Password
8  @li isAdmin
9  @li displayName
10 
11  The userid is created by sasjs/server. All users are created with `isActive`
12  set to `true`.
13 
14  Example:
15 
16  %ms_createuser(newuser,secretpass,displayname=New User!)
17 
18  @param [in] username The username to apply. No spaces or special characters.
19  @param [in] password The initial password to set.
20  @param [in] isadmin= (false) Set to true to give the user admin rights
21  @param [in] displayName= (0) Set a friendly name (spaces & special characters
22  are ok). If not provided, username will be used instead.
23  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
24  @param [out] outds= (work.ms_createuser) This output dataset will contain the
25  values from the JSON response (such as the id of the new user)
26 |ID:best.|DISPLAYNAME:$8.|USERNAME:$8.|ISACTIVE:best.|ISADMIN:best.|
27 |---|---|---|---|---|
28 |`6 `|`New User `|`newuser `|`1 `|`0 `|
29 
30 
31 
32  <h4> SAS Macros </h4>
33  @li mf_getuniquefileref.sas
34  @li mf_getuniquelibref.sas
35  @li mp_abort.sas
36 
37  <h4> Related Files </h4>
38  @li ms_createuser.test.sas
39  @li ms_getusers.sas
40 
41 **/
42 
43 %macro ms_createuser(username,password
44  ,isadmin=false
45  ,displayname=0
46  ,outds=work.ms_createuser
47  ,mdebug=0
48  );
49 
50 %mp_abort(
51  iftrue=(&syscc ne 0)
52  ,mac=ms_createuser.sas
53  ,msg=%str(syscc=&syscc on macro entry)
54 )
55 
56 %local fref0 fref1 fref2 libref optval rc msg;
57 %let fref0=%mf_getuniquefileref();
58 %let fref1=%mf_getuniquefileref();
59 %let fref2=%mf_getuniquefileref();
60 %let libref=%mf_getuniquelibref();
61 
62 /* avoid sending bom marker to API */
63 %let optval=%sysfunc(getoption(bomfile));
64 options nobomfile;
65 
66 data _null_;
67  file &fref0 termstr=crlf;
68  username=quote(cats(symget('username')));
69  password=quote(cats(symget('password')));
70  isadmin=symget('isadmin');
71  displayname=quote(cats(symget('displayname')));
72  if displayname='"0"' then displayname=username;
73 
74 %if &mdebug=1 %then %do;
75  putlog _all_;
76 %end;
77 
78  put '{'@;
79  put '"displayName":' displayname @;
80  put ',"username":' username @;
81  put ',"password":' password @;
82  put ',"isAdmin":' isadmin @;
83  put ',"isActive": true }';
84 run;
85 
86 data _null_;
87  file &fref1 lrecl=1000;
88  infile "&_sasjs_tokenfile" lrecl=1000;
89  input;
90  if _n_=1 then do;
91  put "Content-Type: application/json";
92  put "accept: application/json";
93  end;
94  put _infile_;
95 run;
96 
97 %if &mdebug=1 %then %do;
98  data _null_;
99  infile &fref0;
100  input;
101  put _infile_;
102  data _null_;
103  infile &fref1;
104  input;
105  put _infile_;
106  run;
107 %end;
108 
109 proc http method='POST' in=&fref0 headerin=&fref1 out=&fref2
110  url="&_sasjs_apiserverurl/SASjsApi/user";
111 %if &mdebug=1 %then %do;
112  debug level=1;
113 %end;
114 run;
115 
116 %mp_abort(
117  iftrue=(&syscc ne 0)
118  ,mac=ms_createuser.sas
119  ,msg=%str(Issue submitting query to SASjsApi/user)
120 )
121 
122 libname &libref JSON fileref=&fref2;
123 
124 data &outds;
125  set &libref..root;
126  drop ordinal_root;
127 run;
128 
129 
130 %mp_abort(
131  iftrue=(&syscc ne 0)
132  ,mac=ms_createuser.sas
133  ,msg=%str(Issue reading response JSON)
134 )
135 
136 /* reset options */
137 options &optval;
138 
139 %if &mdebug=1 %then %do;
140  filename &fref0 clear;
141  filename &fref1 clear;
142  filename &fref2 clear;
143  libname &libref clear;
144 %end;
145 
146 %mend ms_createuser;