summaryrefslogtreecommitdiffstats
path: root/idrop-web/grails-app/controllers/org/irods/mydrop/controller/ProfileController.groovy
blob: 39db3d9d34acd8bbe1584d954927260b27673ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.irods.mydrop.controller

import org.irods.jargon.core.connection.IRODSAccount
import org.irods.jargon.core.pub.IRODSAccessObjectFactory
import org.irods.jargon.core.pub.UserAO
import org.irods.jargon.userprofile.UserProfile
import org.irods.mydrop.service.ProfileService
import org.jsoup.Jsoup
import org.jsoup.safety.Whitelist

class ProfileController {
	IRODSAccessObjectFactory irodsAccessObjectFactory
	IRODSAccount irodsAccount
	ProfileService profileService
	def grailsApplication

	/**
	 * Interceptor grabs IRODSAccount from the SecurityContextHolder
	 */
	def beforeInterceptor = [action:this.&auth]

	def auth() {
		if(!session["SPRING_SECURITY_CONTEXT"]) {
			redirect(controller:"login", action:"login")
			return false
		}
		irodsAccount = session["SPRING_SECURITY_CONTEXT"]
	}

	def afterInterceptor = {
		log.debug("closing the session")
		irodsAccessObjectFactory.closeSession()
	}

	/**
	 * Initial listing of profile data, this will create a profile if none exists
	 * @return
	 */
	def index() {
		log.info "index()"
		if (irodsAccount.userName == IRODSAccount.PUBLIC_USERNAME) {
			render(view:"noProfileData")
		} else {
		
			try {
				UserProfile userProfile = profileService.retrieveProfile(irodsAccount)
				ProfileCommand profileCommand = new ProfileCommand()
				profileCommand.userName = Jsoup.clean(userProfile.userName,Whitelist.basic())
				profileCommand.nickName = Jsoup.clean(userProfile.userProfilePublicFields.nickName,Whitelist.basic())
				profileCommand.givenName = Jsoup.clean(userProfile.userProfilePublicFields.givenName,Whitelist.basic())
				profileCommand.lastName = Jsoup.clean(userProfile.userProfilePublicFields.sn,Whitelist.basic())
				profileCommand.city= Jsoup.clean(userProfile.userProfilePublicFields.localityName,Whitelist.basic())
				profileCommand.state= Jsoup.clean(userProfile.userProfilePublicFields.st,Whitelist.basic())
				profileCommand.email = Jsoup.clean(userProfile.userProfileProtectedFields.mail,Whitelist.basic())
				profileCommand.description = Jsoup.clean(userProfile.userProfilePublicFields.description,Whitelist.basic())
				profileCommand.labeledURL = Jsoup.clean(userProfile.userProfilePublicFields.labeledURL,Whitelist.basic())
				profileCommand.postalAddress = Jsoup.clean(userProfile.userProfilePublicFields.postalAddress,Whitelist.basic())
				profileCommand.postalCode = Jsoup.clean(userProfile.userProfilePublicFields.postalCode,Whitelist.basic())
				profileCommand.postOfficeBox = Jsoup.clean(userProfile.userProfilePublicFields.postOfficeBox,Whitelist.basic())
				profileCommand.telephoneNumber = Jsoup.clean(userProfile.userProfilePublicFields.telephoneNumber,Whitelist.basic())
				profileCommand.title = Jsoup.clean(userProfile.userProfilePublicFields.title,Whitelist.basic())
				
				render(view:"index", model:[userProfile:profileCommand])
			} catch (Exception e) {
				response.sendError(500,e.message)
			}
		}
		
	}
	

	/**
	 * Update the profile
	 * @return
	 */
	def updateProfile(ProfileCommand profileCommand) {
		
		log.info("updateProfile")
		log.info "profileCommand: ${profileCommand}"
		
				/**
				 * If there is an error send back the view for redisplay with error messages
				 */
				if (!profileCommand.validate()) {
					log.info("errors in page, returning with error info")
					flash.error =  message(code:"error.data.error")
					render(view:"index", model:[userProfile:profileCommand])
					return
				}
		
				log.info("edits pass")
		

		/*
		 * Massage the params into the user profile
		 */

		
		UserProfile userProfile
		try {
			 userProfile = profileService.retrieveProfile(irodsAccount)
		} catch (Exception e) {
			log.error("error retrieving user profile", e)
			flash.error =  e.message
			render(view:"index", model:[userProfile:profileCommand])
			return
		}
		
		userProfile.userName = irodsAccount.userName
		userProfile.userProfilePublicFields.nickName = Jsoup.clean(profileCommand.nickName,Whitelist.basic())
		userProfile.userProfilePublicFields.description = Jsoup.clean(profileCommand.description,Whitelist.basic())
		userProfile.userProfileProtectedFields.mail = Jsoup.clean(profileCommand.email,Whitelist.basic())
		userProfile.userProfilePublicFields.givenName = Jsoup.clean(profileCommand.givenName,Whitelist.basic())
		userProfile.userProfilePublicFields.sn = Jsoup.clean(profileCommand.lastName,Whitelist.basic())
		userProfile.userProfilePublicFields.localityName = Jsoup.clean(profileCommand.city,Whitelist.basic())
		userProfile.userProfilePublicFields.st = Jsoup.clean(profileCommand.state,Whitelist.basic())
		userProfile.userProfilePublicFields.description = Jsoup.clean(profileCommand.description,Whitelist.basic())
		userProfile.userProfilePublicFields.labeledURL = Jsoup.clean(profileCommand.labeledURL,Whitelist.basic())
		userProfile.userProfilePublicFields.postalAddress = Jsoup.clean(profileCommand.postalAddress,Whitelist.basic())
		userProfile.userProfilePublicFields.postalCode = Jsoup.clean(profileCommand.postalCode,Whitelist.basic())
		userProfile.userProfilePublicFields.postOfficeBox = Jsoup.clean(profileCommand.postOfficeBox,Whitelist.basic())
		userProfile.userProfilePublicFields.telephoneNumber = Jsoup.clean(profileCommand.telephoneNumber,Whitelist.basic())
		userProfile.userProfilePublicFields.title = Jsoup.clean(profileCommand.title,Whitelist.basic())
		
		log.info "updating profile...."
		try {
			profileService.updateProfile(irodsAccount, userProfile)
		} catch (Exception e) {
			log.error("error updating user profile", e)
			flash.error =  e.message
			render(view:"index", model:[userProfile:profileCommand])
			return
		}
		
		log.info "updated"
		flash.message =  message(code:"message.update.successful")
		
		redirect(view:"index", model:[userProfile:profileCommand])
		
	}

	
}

class ProfileCommand {
	String userName
	String nickName
	String givenName
	String lastName
	String city
	String state
	String email
	String description
	String labeledURL
	String postalAddress
	String postalCode
	String postOfficeBox
	String telephoneNumber
	String title
	
	static constraints = {
		nickName(null:false)
		givenName( null:false)
		lastName(null:false)
		city(null:false)
		state(null:false)
		email(null:false)
		description(null:false)
		labeledURL(null:false)
		postalAddress(null:false)
		postalCode(null:false)
		postOfficeBox(null:false) 
		telephoneNumber(null:false)
		title(null:false)
	}
}