Macros for SAS Application Developers
https://github.com/sasjs/core
mm_adduser2group.sas
Go to the documentation of this file.
1 /**
2  @file mm_adduser2group.sas
3  @brief Adds a user to a group
4  @details Adds a user to a metadata group. The macro first checks whether the
5  user is in that group, and if not, the user is added.
6 
7  Note that the macro does not check inherited group memberships - it looks at
8  direct members only.
9 
10  Usage:
11 
12  %mm_adduser2group(user=sasdemo
13  ,group=someGroup)
14 
15 
16  @param [in] user= the user name (not displayname)
17  @param [in] group= the group to which to add the user
18  @param [in] mdebug= (0) set to 1 to show debug info in log
19 
20  <h4> Related Files </h4>
21  @li ms_adduser2group.sas
22 
23  @version 9.3
24  @author Allan Bowe
25 
26 **/
27 
28 %macro mm_adduser2group(user=
29  ,group=
30  ,mdebug=0
31 );
32 /* first, check if user is in group already exists */
33 %local check uuri guri;
34 %let check=ok;
35 
36 data _null_;
37  length uri type msg $256;
38  call missing(of _all_);
39  rc=metadata_getnobj("omsobj:Person?@Name='&user'",1,uri);
40  if rc<=0 then do;
41  msg="%str(WARN)ING: rc="!!cats(rc)!!" &user not found "!!
42  ", or there was an err reading the repository.";
43  call symputx('check',msg);
44  putlog msg;
45  stop;
46  end;
47  call symputx('uuri',scan(uri,2,'\'));
48 
49  rc=metadata_getnobj("omsobj:IdentityGroup?@Name='&group'",1,uri);
50  if rc<=0 then do;
51  msg="%str(WARN)ING: rc="!!cats(rc)!!" &group not found "!!
52  ", or there was an err reading the repository.";
53  call symputx('check',msg);
54  putlog msg;
55  stop;
56  end;
57  call symputx('guri',scan(uri,2,'\'));
58 
59  rc=metadata_getnobj("omsobj:Person?Person[@Name='&user'][IdentityGroups/*[@Name='&group']]",1,uri);
60  if rc=0 then do;
61  msg="%str(WARN)ING: rc="!!cats(rc)!!" &user already in &group";
62  call symputx('check',msg);
63  stop;
64  end;
65 
66  if &mdebug ne 0 then put (_all_)(=);
67 run;
68 
69 /* stop if issues */
70 %if %quote(&check) ne %quote(ok) %then %do;
71  %put &check;
72  %return;
73 %end;
74 
75 %if &syscc ge 4 %then %do;
76  %put %str(WARN)ING: SYSCC=&syscc, exiting &sysmacroname;
77  %return;
78 %end;
79 
80 
81 filename __us2grp temp;
82 
83 proc metadata in= "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata>
84  <Person Id='%nrstr(&uuri)'>
85  <IdentityGroups><IdentityGroup ObjRef='%nrstr(&guri)' />
86  </IdentityGroups></Person></Metadata>
87  <NS>SAS</NS><Flags>268435456</Flags></UpdateMetadata>"
88  out=__us2grp verbose;
89 run;
90 
91 %if &mdebug ne 0 %then %do;
92  /* write the response to the log for debugging */
93  data _null_;
94  infile __us2grp lrecl=32767;
95  input;
96  put _infile_;
97  run;
98 %end;
99 
100 filename __us2grp clear;
101 
102 %mend mm_adduser2group;