Loading...
Searching...
No Matches
ms_getgroups.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Fetches the list of groups from SASjs Server
4 @details Fetches all groups, or the groups for a particular user. When both
5 user and uid are provided, user takes precedence. GROUPURI is the API group
6 uid, not the legacy numeric groupid. Desktop mode returns a synthetic group
7 with GROUPURI=1, GROUPNAME=SYSUSERID and a desktop description.
8
9 A failed HTTP request raises a failure and leaves an empty output dataset,
10 except that a missing user returns no groups. Empty group arrays produce an
11 empty dataset with the model. Descriptions longer than 256 bytes are
12 truncated to fit GROUPDESC.
13
14 Example:
15
16 %ms_getgroups(outds=work.groups)
17 %ms_getgroups(user=James,outds=work.groups)
18 %ms_getgroups(uid=1,outds=work.groups)
19
20 @param [in] mdebug= (0) Set to 1 to enable HTTP debug messages
21 @param [in] user= (0) Username used to filter groups
22 @param [in] uid= (0) User uid used to filter groups
23 @param [out] outds= (work.ms_getgroups) Dataset with exactly GROUPURI,
24 GROUPNAME and GROUPDESC, all character length 256.
25
26 <h4> SAS Macros </h4>
27 @li mddl_getgroups.sas
28 @li mf_getuniquefileref.sas
29 @li mf_getuniquelibref.sas
30 @li mf_getuniquename.sas
31 @li mp_abort.sas
32
33 <h4> Related Files </h4>
34 @li ms_creategroup.sas
35 @li ms_getgroups.test.sas
36
37**/
38%macro ms_getgroups(user=0,uid=0,outds=work.ms_getgroups,mdebug=0);
39%local fref0 fref1 libref optval url table dsid rc schema;
40
41%mp_abort(iftrue=(&syscc ne 0),mac=ms_getgroups.sas
42 ,msg=%str(syscc=&syscc on macro entry))
43
44%let schema=work.%mf_getuniquename(prefix=ms_getgroups);
45%mddl_getgroups(&schema)
46
47data &outds;
48 if 0 then set &schema;
49 call missing(groupuri,groupname,groupdesc);
50 stop;
51run;
52
53%if %sysget(MODE)=desktop %then %do;
54 data &outds;
55 if 0 then set &schema;
56 groupuri='1';
57 groupname=symget('sysuserid');
58 groupdesc=cats(symget('sysuserid'),' (group - desktop mode)');
59 output;
60 stop;
61 run;
62 %return;
63%end;
64
65%let fref0=%mf_getuniquefileref();
66%let fref1=%mf_getuniquefileref();
67%let libref=%mf_getuniquelibref();
68%let optval=%sysfunc(getoption(bomfile));
69options nobomfile;
70
71data _null_;
72 file &fref0 lrecl=1000;
73 infile "&_sasjs_tokenfile" lrecl=1000;
74 input;
75 if _n_=1 then put 'accept: application/json';
76 put _infile_;
77run;
78
79%if %superq(user) ne 0 %then %let url=/SASjsApi/user/by/username/&user;
80%else %if %superq(uid) ne 0 %then %let url=/SASjsApi/user/&uid;
81%else %let url=/SASjsApi/group;
82
83proc http method='GET' headerin=&fref0 out=&fref1
84 url="&_sasjs_apiserverurl.&url";
85%if &mdebug=1 %then %do;
86 debug level=1;
87%end;
88run;
89options &optval;
90
91%if &syscc ne 0 or %superq(SYS_PROCHTTP_STATUS_CODE) ne 200 %then %do;
92 /* a filtered query for an unknown user or uid is not a failure - the output
93 keeps the model with zero rows, matching Viya's unknown-user behaviour */
94 %if (%superq(user) ne 0 or %superq(uid) ne 0)
95 and %superq(SYS_PROCHTTP_STATUS_CODE)=404 %then %do;
96 %put NOTE: MS_GETGROUPS user or uid not found, no groups returned.;
97 %end;
98 %else %do;
99 %put %str(ERR)OR: MS_GETGROUPS HTTP status &SYS_PROCHTTP_STATUS_CODE.;
100 %let syscc=8;
101 %end;
102 filename &fref0 clear;
103 filename &fref1 clear;
104 %return;
105%end;
106
107libname &libref JSON fileref=&fref1;
108%if %superq(user)=0 and %superq(uid)=0 %then %let table=root;
109%else %let table=groups;
110
111/* Empty arrays have no group fields in the JSON engine's table. */
112%let dsid=%sysfunc(open(&libref..&table,i));
113%if &dsid %then %do;
114 %let rc=%sysfunc(varnum(&dsid,uid));
115 %let dsid=%sysfunc(close(&dsid));
116 %if &rc %then %do;
117 data &outds;
118 if 0 then set &schema;
119 set &libref..&table(keep=uid name description);
120 groupuri=uid;
121 groupname=name;
122 groupdesc=description;
123 keep groupuri groupname groupdesc;
124 run;
125 %end;
126%end;
127%else %do;
128 %put %str(ERR)OR: MS_GETGROUPS unable to open JSON table &table.;
129 %let syscc=8;
130%end;
131libname &libref clear;
132filename &fref0 clear;
133filename &fref1 clear;
134%mend ms_getgroups;