/
ProtoUser.scala
428 lines (360 loc) · 11.9 KB
/
ProtoUser.scala
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package record
import net.liftweb.http.S
import net.liftweb.http.S._
import net.liftweb.http.js._
import JsCmds._
import scala.xml.{NodeSeq, Node, Text, Elem}
import scala.xml.transform._
import net.liftweb.sitemap._
import net.liftweb.sitemap.Loc._
import net.liftweb.util.Helpers._
import net.liftweb.util._
import net.liftweb.common._
import net.liftweb.util.Mailer._
import net.liftweb.record.field._
import net.liftweb.proto.{ProtoUser => GenProtoUser}
/**
* ProtoUser provides a "User" with a first name, last name, email, etc.
*/
trait ProtoUser[T <: ProtoUser[T]] extends Record[T] {
self: T =>
/**
* The primary key field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val id = new MyMappedLongClass(this) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val id: LongField[T] = new MyMappedLongClass(this)
protected class MyMappedLongClass(obj: T) extends LongField(obj)
/**
* Convert the id to a String
*/
def userIdAsString: String = id.get.toString
/**
* The first name field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val firstName = new MyFirstName(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val firstName: StringField[T] = new MyFirstName(this, 32)
protected class MyFirstName(obj: T, size: Int) extends StringField(obj, size) {
override def displayName = owner.firstNameDisplayName
override val fieldId = Some(Text("txtFirstName"))
}
/**
* The string name for the first name field
*/
def firstNameDisplayName = S.?("first.name")
/**
* The last field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val lastName = new MyLastName(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val lastName: StringField[T] = new MyLastName(this, 32)
protected class MyLastName(obj: T, size: Int) extends StringField(obj, size) {
override def displayName = owner.lastNameDisplayName
override val fieldId = Some(Text("txtLastName"))
}
/**
* The last name string
*/
def lastNameDisplayName = S.?("last.name")
/**
* The email field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val email = new MyEmail(this, 48) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val email: EmailField[T] = new MyEmail(this, 48)
protected class MyEmail(obj: T, size: Int) extends EmailField(obj, size) {
override def validations = valUnique(S.?("unique.email.address")) _ :: super.validations
override def displayName = owner.emailDisplayName
override val fieldId = Some(Text("txtEmail"))
}
protected def valUnique(errorMsg: => String)(email: String): List[FieldError]
/**
* The email first name
*/
def emailDisplayName = S.?("email.address")
/**
* The password field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val password = new MyPassword(this) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val password: PasswordField[T] = new MyPassword(this)
protected class MyPassword(obj: T) extends PasswordField(obj) {
override def displayName = owner.passwordDisplayName
}
/**
* The display name for the password field
*/
def passwordDisplayName = S.?("password")
/**
* The superuser field for the User. You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val superUser = new MySuperUser(this) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val superUser: BooleanField[T] = new MySuperUser(this)
protected class MySuperUser(obj: T) extends BooleanField(obj) {
override def defaultValue = false
}
def niceName: String = (firstName.get, lastName.get, email.get) match {
case (f, l, e) if f.length > 1 && l.length > 1 => f+" "+l+" ("+e+")"
case (f, _, e) if f.length > 1 => f+" ("+e+")"
case (_, l, e) if l.length > 1 => l+" ("+e+")"
case (_, _, e) => e
}
def shortName: String = (firstName.get, lastName.get) match {
case (f, l) if f.length > 1 && l.length > 1 => f+" "+l
case (f, _) if f.length > 1 => f
case (_, l) if l.length > 1 => l
case _ => email.get
}
def niceNameWEmailLink = <a href={"mailto:"+email.get}>{niceName}</a>
}
/**
* Mix this trait into the Mapper singleton for User and you
* get a bunch of user functionality including password reset, etc.
*/
trait MetaMegaProtoUser[ModelType <: MegaProtoUser[ModelType]] extends MetaRecord[ModelType] with GenProtoUser {
self: ModelType =>
type TheUserType = ModelType
/**
* What's a field pointer for the underlying CRUDify
*/
type FieldPointerType = Field[_, TheUserType]
/**
* Based on a FieldPointer, build a FieldPointerBridge
*/
protected implicit def buildFieldBridge(from: FieldPointerType): FieldPointerBridge = new MyPointer(from)
protected class MyPointer(from: FieldPointerType) extends FieldPointerBridge {
/**
* What is the display name of this field?
*/
def displayHtml: NodeSeq = from.displayHtml
/**
* Does this represent a pointer to a Password field?
*/
def isPasswordField_? : Boolean = from match {
case a: PasswordField[_] => true
case _ => false
}
}
/**
* Convert an instance of TheUserType to the Bridge trait
*/
protected implicit def typeToBridge(in: TheUserType): UserBridge =
new MyUserBridge(in)
/**
* Bridges from TheUserType to methods used in this class
*/
protected class MyUserBridge(in: TheUserType) extends UserBridge {
/**
* Convert the user's primary key to a String
*/
def userIdAsString: String = in.id.toString
/**
* Return the user's first name
*/
def getFirstName: String = in.firstName.get
/**
* Return the user's last name
*/
def getLastName: String = in.lastName.get
/**
* Get the user's email
*/
def getEmail: String = in.email.get
/**
* Is the user a superuser
*/
def superUser_? : Boolean = in.superUser.get
/**
* Has the user been validated?
*/
def validated_? : Boolean = in.validated.get
/**
* Does the supplied password match the actual password?
*/
def testPassword(toTest: Box[String]): Boolean =
toTest.map(in.password.match_?) openOr false
/**
* Set the validation flag on the user and return the user
*/
def setValidated(validation: Boolean): TheUserType =
in.validated(validation)
/**
* Set the unique ID for this user to a new value
*/
def resetUniqueId(): TheUserType = {
in.uniqueId.reset()
}
/**
* Return the unique ID for the user
*/
def getUniqueId(): String = in.uniqueId.get
/**
* Validate the user
*/
def validate: List[FieldError] = in.validate
/**
* Given a list of string, set the password
*/
def setPasswordFromListString(pwd: List[String]): TheUserType = {
in.password.setFromAny(pwd)
in
}
/**
* Save the user to backing store
*/
def save(): Boolean = in.saveTheRecord().isDefined
}
/**
* Given a field pointer and an instance, get the field on that instance
*/
protected def computeFieldFromPointer(instance: TheUserType, pointer: FieldPointerType): Box[BaseField] = fieldByName(pointer.name, instance)
/**
* Given an username (probably email address), find the user
*/
protected def findUserByUserName(email: String): Box[TheUserType]
/**
* Given a unique id, find the user
*/
protected def findUserByUniqueId(id: String): Box[TheUserType]
/**
* Create a new instance of the User
*/
protected def createNewUserInstance(): TheUserType = self.createRecord
/**
* Given a String representing the User ID, find the user
*/
protected def userFromStringId(id: String): Box[TheUserType]
/**
* The list of fields presented to the user at sign-up
*/
def signupFields: List[FieldPointerType] = List(firstName,
lastName,
email,
locale,
timezone,
password)
/**
* The list of fields presented to the user for editing
*/
def editFields: List[FieldPointerType] = List(firstName,
lastName,
email,
locale,
timezone)
}
/**
* ProtoUser is bare bones. MetaProtoUser contains a bunch
* more fields including a validated flag, locale, timezone, etc.
*/
trait MegaProtoUser[T <: MegaProtoUser[T]] extends ProtoUser[T] {
self: T =>
/**
* The unique id field for the User. This field
* is used for validation, lost passwords, etc.
* You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val uniqueId = new MyUniqueId(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val uniqueId: UniqueIdField[T] = new MyUniqueId(this, 32)
protected class MyUniqueId(obj: T, size: Int) extends UniqueIdField(obj, size) {
}
/**
* Whether the user has been validated.
* You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val validated = new MyValidated(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val validated: BooleanField[T] = new MyValidated(this)
protected class MyValidated(obj: T) extends BooleanField[T](obj) {
override def defaultValue = false
override val fieldId = Some(Text("txtValidated"))
}
/**
* The locale field for the User.
* You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val locale = new MyLocale(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val locale: LocaleField[T] = new MyLocale(this)
protected class MyLocale(obj: T) extends LocaleField[T](obj) {
override def displayName = owner.localeDisplayName
override val fieldId = Some(Text("txtLocale"))
}
/**
* The time zone field for the User.
* You can override the behavior
* of this field:
* <pre name="code" class="scala">
* override lazy val timezone = new MyTimeZone(this, 32) {
* println("I am doing something different")
* }
* </pre>
*/
lazy val timezone: TimeZoneField[T] = new MyTimeZone(this)
protected class MyTimeZone(obj: T) extends TimeZoneField[T](obj) {
override def displayName = owner.timezoneDisplayName
override val fieldId = Some(Text("txtTimeZone"))
}
/**
* The string for the timezone field
*/
def timezoneDisplayName = S.?("time.zone")
/**
* The string for the locale field
*/
def localeDisplayName = S.?("locale")
}