1 | package edu.iu.uis.sit.portal.user.service; |
2 | |
3 | import java.io.FileInputStream; |
4 | import java.util.List; |
5 | import java.util.Properties; |
6 | |
7 | import edu.iu.uis.my.mygds.GdsClient; |
8 | import edu.iu.uis.sit.portal.portlet.utility.Utility; |
9 | import edu.iu.uis.sit.util.directory.AdsHelper; |
10 | import edu.iu.uis.sit.util.directory.AdsPerson; |
11 | import edu.iu.uis.sit.util.directory.gds.GdsPerson; |
12 | import edu.iu.uis.sit.util.directory.gds.IUEduPSEMPLID; |
13 | import edu.iu.uis.sit.util.directory.gds.NetworkId; |
14 | |
15 | public class AclServiceImpl implements AclService { |
16 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AclServiceImpl.class); |
17 | |
18 | private static AdsHelper adsHelper; |
19 | |
20 | private static GdsClient gdsClient; |
21 | |
22 | private AdsHelper getAdsHelper() { |
23 | if (adsHelper == null) { |
24 | try { |
25 | Properties securityProperties = new Properties(); |
26 | securityProperties.load(new FileInputStream(Utility.getPathToSecurity() + "MYADS.properties")); |
27 | adsHelper = new AdsHelper(securityProperties.getProperty("username"), securityProperties.getProperty("password")); |
28 | } catch (Exception e) { |
29 | LOG.error("error retrieving groups", e); |
30 | } |
31 | } |
32 | return adsHelper; |
33 | } |
34 | |
35 | private GdsClient getGdsClient() { |
36 | if (gdsClient == null) { |
37 | try { |
38 | Properties settingsProperties = new Properties(); |
39 | Properties securityProperties = new Properties(); |
40 | settingsProperties.load(new FileInputStream(Utility.getPathToSettings() + "MYGDS.properties")); |
41 | securityProperties.load(new FileInputStream(Utility.getPathToSecurity() + "MYGDS.properties")); |
42 | gdsClient = GdsClient.initGdsClient(settingsProperties.getProperty("ldapUrl"), securityProperties.getProperty("username"), securityProperties.getProperty("password")); |
43 | } catch (Exception e) { |
44 | LOG.error("Error initing GDS Client", e); |
45 | } |
46 | } |
47 | return gdsClient; |
48 | } |
49 | |
50 | private List getGroups(String username) throws Exception { |
51 | AdsHelper helper = getAdsHelper(); |
52 | return helper.getGroups(username, AdsHelper.INFINITE_GROUP_DEPTH); |
53 | } |
54 | |
55 | private AdsPerson getAdsPerson(String username) throws Exception { |
56 | AdsHelper helper = getAdsHelper(); |
57 | AdsPerson adsPerson = helper.getAdsPerson(username); |
58 | return adsPerson; |
59 | } |
60 | |
61 | public AclPerson findUserByPersonId(String personId) { |
62 | IUEduPSEMPLID person = new IUEduPSEMPLID(personId); |
63 | AclPerson aclPerson = new AclPerson(); |
64 | try { |
65 | GdsPerson gdsPerson = getGdsClient().fetchGdsUser(person); |
66 | AdsPerson adsPerson = getAdsPerson(gdsPerson.getUid()); |
67 | aclPerson.setEmailAddress(adsPerson.getMail()); |
68 | aclPerson.setFirstName(gdsPerson.getGivenName()); |
69 | aclPerson.setLastName(gdsPerson.getSn()); |
70 | aclPerson.setUserName(gdsPerson.getUid()); |
71 | aclPerson.setCampus(gdsPerson.getOu()); |
72 | aclPerson.setPersonId(gdsPerson.getIuEduPSEMPLID()); |
73 | aclPerson.setGroups(getGroups(gdsPerson.getUid())); |
74 | } catch (Exception e) { |
75 | LOG.error("error in getting gds person", e); |
76 | } |
77 | return aclPerson; |
78 | } |
79 | |
80 | public AclPerson findUserByUsername(String userName) { |
81 | NetworkId person = new NetworkId(userName); |
82 | AclPerson aclPerson = new AclPerson(); |
83 | try { |
84 | GdsPerson gdsPerson = getGdsClient().fetchGdsUser(person); |
85 | aclPerson.setEmailAddress(gdsPerson.getMail()); |
86 | aclPerson.setFirstName(gdsPerson.getGivenName()); |
87 | aclPerson.setLastName(gdsPerson.getSn()); |
88 | aclPerson.setUserName(gdsPerson.getUid()); |
89 | aclPerson.setPersonId(gdsPerson.getIuEduPSEMPLID()); |
90 | aclPerson.setCampus(gdsPerson.getOu()); |
91 | aclPerson.setGroups(getGroups(gdsPerson.getUid())); |
92 | } catch (Exception e) { |
93 | LOG.error("error in getting gds person", e); |
94 | } |
95 | return aclPerson; |
96 | } |
97 | |
98 | public boolean isValidGroup(String group) { |
99 | try { |
100 | return getAdsHelper().isValidGroupName(group); |
101 | } catch (Exception e) { |
102 | LOG.error("error finding group: " + group); |
103 | return false; |
104 | } |
105 | } |
106 | } |