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