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