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