Macros for SAS Application Developers
https://github.com/sasjs/core
ms_getusers.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Fetches the list of users from SASjs Server
4  @details Fetches the list of users from SASjs Server and writes them to an
5  output dataset. Can also be filtered, for a particular group.
6 
7  Example:
8 
9  %ms_getusers(outds=userlist)
10 
11  Filtering for a group by group name:
12 
13  %ms_getusers(outds=work.groupmembers, group=GROUPNAME)
14 
15  Filtering for a group by group id:
16 
17  %ms_getusers(outds=work.groupmembers, gid=1)
18 
19  @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
20  @param [in] group= (0) Set to a group name to filter members for that group
21  @param [in] gid= (0) Set to a group id to filter members for that group
22  @param [out] outds= (work.ms_getusers) This output dataset will contain the
23  list of user accounts. Format:
24 |DISPLAYNAME:$60.|USERNAME:$30.|ID:best.|
25 |---|---|---|
26 |`Super Admin `|`secretuser `|`1`|
27 |`Sabir Hassan`|`sabir`|`2`|
28 |`Mihajlo Medjedovic `|`mihajlo `|`3`|
29 |`Ivor Townsend `|`ivor `|`4`|
30 |`New User `|`newuser `|`5`|
31 
32 
33  <h4> SAS Macros </h4>
34  @li mf_getuniquefileref.sas
35  @li mf_getuniquelibref.sas
36  @li mp_abort.sas
37 
38  <h4> Related Files </h4>
39  @li ms_createuser.sas
40  @li ms_getgroups.sas
41  @li ms_getusers.test.sas
42 
43 **/
44 
45 %macro ms_getusers(
46  outds=work.ms_getusers,
47  group=0,
48  gid=0,
49  mdebug=0
50 );
51 
52 %mp_abort(
53  iftrue=(&syscc ne 0)
54  ,mac=ms_getusers.sas
55  ,msg=%str(syscc=&syscc on macro entry)
56 )
57 
58 %local fref0 fref1 libref optval rc msg url;
59 %let fref0=%mf_getuniquefileref();
60 %let fref1=%mf_getuniquefileref();
61 %let libref=%mf_getuniquelibref();
62 
63 %if %sysget(MODE)=desktop %then %do;
64  /* users api does not exist in desktop mode */
65  data &outds;
66  length DISPLAYNAME $60 USERNAME:$30 ID 8;
67  USERNAME="&sysuserid";
68  DISPLAYNAME="&sysuserid (desktop mode)";
69  ID=1;
70  output;
71  stop;
72  run;
73  %return;
74 %end;
75 
76 /* avoid sending bom marker to API */
77 %let optval=%sysfunc(getoption(bomfile));
78 options nobomfile;
79 
80 data _null_;
81  file &fref0 lrecl=1000;
82  infile "&_sasjs_tokenfile" lrecl=1000;
83  input;
84  if _n_=1 then put "accept: application/json";
85  put _infile_;
86 run;
87 
88 %if &mdebug=1 %then %do;
89  data _null_;
90  infile &fref0;
91  input;
92  put _infile_;
93  run;
94 %end;
95 
96 %if "&group" ne "0" %then %let url=/SASjsApi/group/by/groupname/&group;
97 %else %if "&gid" ne "0" %then %let url=/SASjsApi/group/&gid;
98 %else %let url=/SASjsApi/user;
99 
100 proc http method='GET' headerin=&fref0 out=&fref1
101  url="&_sasjs_apiserverurl.&url";
102 %if &mdebug=1 %then %do;
103  debug level=1;
104 %end;
105 run;
106 
107 
108 %mp_abort(
109  iftrue=(&syscc ne 0)
110  ,mac=ms_getusers.sas
111  ,msg=%str(Issue submitting API query)
112 )
113 
114 libname &libref JSON fileref=&fref1;
115 
116 %if "&group"="0" and "&gid"="0" %then %do;
117  data &outds;
118  length DISPLAYNAME $60 USERNAME:$30 ID 8;
119  if nobs=0 then call missing(of _all_);
120  set &libref..root nobs=nobs;
121  drop ordinal_root;
122  run;
123 %end;
124 %else %do;
125  data &outds;
126  length DISPLAYNAME $60 USERNAME:$30 ID 8;
127  if nobs=0 then call missing(of _all_);
128  set &libref..users nobs=nobs;
129  drop ordinal_root ordinal_users;
130  run;
131 %end;
132 
133 %mp_abort(
134  iftrue=(&syscc ne 0)
135  ,mac=ms_getusers.sas
136  ,msg=%str(Issue reading response JSON)
137 )
138 
139 /* reset options */
140 options &optval;
141 
142 %if &mdebug=1 %then %do;
143  filename &fref0 clear;
144  filename &fref1 clear;
145  libname &libref clear;
146 %end;
147 
148 %mend ms_getusers;