Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_getgroups.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates dataset with all groups or just those for a particular user
4  @details Provide a metadata user to get groups for just that user, or leave
5  blank to return all groups.
6  Usage:
7 
8  - all groups: `%mm_getGroups()`
9 
10  - all groups for a particular user: `%mm_getgroups(user=&sysuserid)`
11 
12  @param [in] user= the metadata user to return groups for. Leave blank for all
13  groups.
14  @param [in] repo= the metadata repository that contains the user/group information
15  @param [in] mDebug= set to 1 to show debug messages in the log
16  @param [out] outds= the dataset to create that contains the list of groups
17 
18  @returns outds dataset containing all groups in a column named "metagroup"
19  - groupuri
20  - groupname
21  - groupdesc
22 
23  @version 9.2
24  @author Allan Bowe
25 
26 **/
27 
28 %macro mm_getGroups(
29  user=
30  ,outds=work.mm_getGroups
31  ,repo=foundation
32  ,mDebug=0
33 )/*/STORE SOURCE*/;
34 
35 %local mD oldrepo;
36 %let oldrepo=%sysfunc(getoption(metarepository));
37 %if &mDebug=1 %then %let mD=;
38 %else %let mD=%str(*);
39 %&mD.put Executing mm_getGroups.sas;
40 %&mD.put _local_;
41 
42 /* on some sites, user / group info is in a different metadata repo to the default */
43 %if &oldrepo ne &repo %then %do;
44  options metarepository=&repo;
45 %end;
46 
47 %if %length(&user)=0 %then %do;
48  data &outds (keep=groupuri groupname groupdesc);
49  length groupuri groupname groupdesc group_or_role $256;
50  call missing(of _all_);
51  i+1;
52  do while
53  (metadata_getnobj("omsobj:IdentityGroup?@Id contains '.'",i,groupuri)>0);
54  rc=metadata_getattr(groupuri, "Name", groupname);
55  rc=metadata_getattr(groupuri, "Desc", groupdesc);
56  rc=metadata_getattr(groupuri,"PublicType",group_or_role);
57  if Group_or_Role = 'UserGroup' then output;
58  i+1;
59  end;
60  run;
61 %end;
62 %else %do;
63  data &outds (keep=groupuri groupname groupdesc);
64  length uri groupuri groupname groupdesc group_or_role $256;
65  call missing(of _all_);
66  rc=metadata_getnobj("omsobj:Person?@Name='&user'",1,uri);
67  if rc<=0 then do;
68  putlog "%str(WARN)ING: rc=" rc "&user not found "
69  ", or there was an issue reading the repository.";
70  stop;
71  end;
72  a=1;
73  grpassn=metadata_getnasn(uri,"IdentityGroups",a,groupuri);
74  if grpassn in (-3,-4) then do;
75  putlog "%str(WARN)ING: No metadata groups found for &user";
76  output;
77  end;
78  else do while (grpassn > 0);
79  rc=metadata_getattr(groupuri, "Name", groupname);
80  rc=metadata_getattr(groupuri, "Desc", groupdesc);
81  a+1;
82  rc=metadata_getattr(groupuri,"PublicType",group_or_role);
83  if Group_or_Role = 'UserGroup' then output;
84  grpassn=metadata_getnasn(uri,"IdentityGroups",a,groupuri);
85  end;
86  run;
87 %end;
88 
89 %if &oldrepo ne &repo %then %do;
90  options metarepository=&oldrepo;
91 %end;
92 
93 %mend;