Skip to content

Commit

Permalink
email required, and notifications sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivar Abrahamsen committed Jun 10, 2012
1 parent 06eae1c commit aaeb7be
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 109 deletions.
6 changes: 3 additions & 3 deletions app/controllers/AlbumController.scala
Expand Up @@ -35,7 +35,7 @@ object AlbumController extends Controller with EventWrappers with Secured {
},
submittedAlbumForm => {
val album = new Album(participant.username,submittedAlbumForm._2)
// EmailNotifier.addAlbumNotification(participant,event,album)
EmailNotifier.addAlbumNotification(participant,event,album)
event.addAlbum(album)
Redirect(routes.EventController.viewEvent(eventId)).flashing("message" -> "Album added")
}
Expand All @@ -49,7 +49,7 @@ object AlbumController extends Controller with EventWrappers with Secured {
}


def updateAlbum(eventId: Long,albumId: Long) = isEventParticipant(eventId) { (event,participant) => implicit request =>
def updateAlbum(eventId: Long,albumId: Long) = isEventParticipantOrAdmin(eventId) { (event,participant) => implicit request =>
event.findAlbum(albumId) match {
case None => albumNotFound
case Some(album) => {
Expand All @@ -75,7 +75,7 @@ object AlbumController extends Controller with EventWrappers with Secured {
}


def removeAlbum(eventId: Long, albumId: Long) = isEventParticipant(eventId) { (event,participant) => implicit request =>
def removeAlbum(eventId: Long, albumId: Long) = isEventParticipantOrAdmin(eventId) { (event,participant) => implicit request =>
event.findAlbum(albumId) match {
case None => albumNotFound
case Some(album) => {
Expand Down
38 changes: 30 additions & 8 deletions app/controllers/Application.scala
Expand Up @@ -9,7 +9,6 @@ import notifiers._

object Application extends Controller with Secured {

val ValidEmailAddress = """^[0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$""".r

val loginForm = Form(
tuple(
Expand All @@ -24,7 +23,7 @@ object Application extends Controller with Secured {
tuple(
"username" -> nonEmptyText(maxLength = 99),
"fullname" -> optional(text(maxLength = 99)),
"email" -> optional(text(maxLength = 99)),
"email" -> nonEmptyText(maxLength = 99),
"password" -> nonEmptyText(minLength = 4, maxLength = 99),
"confirm" -> nonEmptyText(minLength = 4, maxLength = 99)
) verifying("Passwords do not match", fields => fields match {
Expand All @@ -36,12 +35,20 @@ object Application extends Controller with Secured {
!Participant.findByUsername(username.trim).isDefined
}
}) verifying("Email address is not valid", fields => fields match {
case (username, fullname, email, password, confirmPassword) => {
email match {
case Some(emailAddress) => ValidEmailAddress.findFirstIn(emailAddress.trim).isDefined
case None => true
}
case (username, fullname, email, password, confirmPassword) => Participant.ValidEmailAddress.findFirstIn(email.trim).isDefined
})
)

val initialRegisterForm = Form(
tuple(
"username" -> nonEmptyText(maxLength = 99),
"email" -> nonEmptyText(maxLength = 99)
) verifying("Username is already taken", fields => fields match {
case (username, email) => {
!Participant.findByUsername(username.trim).isDefined
}
}) verifying("Email address is not valid", fields => fields match {
case (username, email) => Participant.ValidEmailAddress.findFirstIn(email.trim).isDefined
})
)

Expand Down Expand Up @@ -86,6 +93,20 @@ object Application extends Controller with Secured {
Ok(views.html.register(registerForm))
}

def firstRegisterStep = Action {
implicit request =>
initialRegisterForm.bindFromRequest.fold(
errors => {
Logger.warn("Registration failed: " + errors)
BadRequest(views.html.register(registerForm.fill(errors.get._1,None,errors.get._2,"","")))
},
registeredForm => {
Ok(views.html.register(registerForm.fill(registeredForm._1,None,registeredForm._2,"",""))).flashing("message"->"Please fill in your name and choose a password")
}
)
}


def register = Action {
implicit request =>
registerForm.bindFromRequest.fold(
Expand All @@ -97,13 +118,14 @@ object Application extends Controller with Secured {
if (Logger.isDebugEnabled) Logger.debug("Registering: " + registeredForm._1)
val participant = Participant(0, registeredForm._1, registeredForm._2, registeredForm._3, Some(registeredForm._4))
Participant.save(participant)
EmailNotifier.registrationNotification(participant)
EmailNotifier.registrationAlert(participant)
Redirect(routes.Application.index()).withSession("username" -> participant.username).flashing("message" -> "Registered. Welcome")
}
)
}



}


Expand Down
33 changes: 22 additions & 11 deletions app/controllers/EventController.scala
Expand Up @@ -79,7 +79,7 @@ object EventController extends Controller with EventWrappers with Secured {
currentParticipant match {
case None => eventRequireAuthentication(eventId)
case Some(participant) => {
if(event.isParticipant(participant) ) {
if(event.isParticipant(participant) || participant.isAdmin) {
Ok(views.html.events.view(event,albums,participants))
} else {
notEventParticipant(event)
Expand All @@ -97,15 +97,15 @@ object EventController extends Controller with EventWrappers with Secured {
BadRequest(views.html.index(searchForm,errors,Application.registerForm))
},
eventName => {
EmailNotifier.createEventNotification(participant,eventName)
EmailNotifier.createEventAlert(participant,eventName)
val event = participant.createAndSaveEvent(eventName)
Redirect(routes.EventController.showEditEvent(event.eventId)).flashing("message" -> "Event created")
}
)
}


def showEditEvent(eventId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def showEditEvent(eventId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
val editForm = updateForm.fill((event.eventName,
event.organiser match {
case None => ""
Expand All @@ -122,7 +122,7 @@ object EventController extends Controller with EventWrappers with Secured {
}


def updateEvent(eventId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def updateEvent(eventId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
updateForm.bindFromRequest.fold(
errors => {
Logger.warn("Bad update event request:"+errors)
Expand All @@ -146,13 +146,14 @@ object EventController extends Controller with EventWrappers with Secured {
}


def showDeleteEvent(eventId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def showDeleteEvent(eventId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
Ok(views.html.events.delete(event))
}


def deleteEvent(eventId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def deleteEvent(eventId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
Logger.info("Deleting event: " + eventId)
EmailNotifier.deleteEventAlert(participant,event)
EmailNotifier.deleteEventNotification(participant,event)
Event.deleteEvent(eventId)
Redirect(routes.Application.index()).flashing("message" -> "Event deleted");
Expand All @@ -165,7 +166,7 @@ object EventController extends Controller with EventWrappers with Secured {
}


def removeParticipant(eventId: Long, participantId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def removeParticipant(eventId: Long, participantId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
Logger.debug("Remove participant: " + participantId +" from "+eventId)
Participant.findById(participantId) match {
case None => eventParticipantNotFound(eventId)
Expand All @@ -178,7 +179,7 @@ object EventController extends Controller with EventWrappers with Secured {
}


def addCurrentParticipant(eventId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def addCurrentParticipant(eventId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
participantForm.bindFromRequest.fold(
errors => {
for(error<-errors.errors){
Expand All @@ -194,7 +195,7 @@ object EventController extends Controller with EventWrappers with Secured {
}


def addRequestedParticipant(eventId: Long, participantId: Long) = isEventOrganiser(eventId) { (event,participant) => implicit request =>
def addRequestedParticipant(eventId: Long, participantId: Long) = isEventOrganiserOrAdmin(eventId) { (event,participant) => implicit request =>
addAnyParticipant(event,Participant.findById(participantId))
}

Expand All @@ -213,6 +214,7 @@ object EventController extends Controller with EventWrappers with Secured {
def requestToJoin(eventId: Long) = withEventAndParticipant(eventId) { (event,participant) => implicit request =>
Logger.debug("Request to join:"+eventId)
event.addJoinRequest(participant)
EmailNotifier.sendJoinRequestNotification(event,participant)
if(event.public || !event.organiser.isDefined || event.isParticipant(participant) ){
Redirect( routes.EventController.viewEvent(eventId) ).flashing(("message"->"Request to join sent"),("eventId"->eventId.toString))
} else {
Expand Down Expand Up @@ -243,9 +245,9 @@ trait EventWrappers extends Secured {
}.getOrElse(onEventNotFound(request))
}

def isEventOrganiser(eventId: Long)(f: (Event,Participant) => Request[AnyContent] => Result) = withEventAndParticipant(eventId) {
def isEventOrganiserOrAdmin(eventId: Long)(f: (Event,Participant) => Request[AnyContent] => Result) = withEventAndParticipant(eventId) {
(event,participant) => implicit request =>
if( event.isOrganiser(participant) ){
if( event.isOrganiser(participant) || participant.isAdmin){
f(event,participant)(request)
} else {
onUnauthorised(request,event)(request.session,request.flash)
Expand All @@ -261,4 +263,13 @@ trait EventWrappers extends Secured {
}
}

def isEventParticipantOrAdmin(eventId: Long)(f: (Event,Participant) => Request[AnyContent] => Result) = withEventAndParticipant(eventId) {
(event,participant) => implicit request =>
if( event.isParticipant(participant) || participant.isAdmin){
f(event,participant)(request)
} else {
onUnauthorised(request,event)(request.session,request.flash)
}
}

}
14 changes: 4 additions & 10 deletions app/controllers/ParticipantController.scala
Expand Up @@ -12,8 +12,6 @@ import notifiers.EmailNotifier

object ParticipantController extends Controller with Secured {



val resetForm = Form(
"username" -> nonEmptyText(maxLength = 99)
)
Expand All @@ -26,15 +24,11 @@ object ParticipantController extends Controller with Secured {
tuple(
"username" -> nonEmptyText(maxLength = 99),
"fullname" -> optional(text(maxLength = 99)),
"email" -> optional(text(maxLength = 99))
"email" -> text(maxLength = 99)
) verifying("Email address is not valid", fields => fields match {
case (username, fullname, email ) => {
email match {
case Some(emailAddress) => Application.ValidEmailAddress.findFirstIn(emailAddress.trim).isDefined
case None => true
}
case (username, fullname, email ) => Participant.ValidEmailAddress.findFirstIn(email.trim).isDefined
}
})
)
)

val passwordForm = Form(
Expand Down Expand Up @@ -99,7 +93,7 @@ object ParticipantController extends Controller with Secured {
def deleteParticipant(participantId: Long) = withParticipant { participant => implicit request =>
if(participant.participantId == participantId){
Logger.info("Participant deleting:" + participantId + " | " + participant.username)
EmailNotifier.deleteParticipantNotification(participant)
EmailNotifier.deleteParticipantAlert(participant)
Participant.deleteAccount(participantId)
Logger.warn("Participant deleted:" + participantId + " | " + participant.username)
Redirect(routes.Application.index()).withNewSession;
Expand Down
8 changes: 6 additions & 2 deletions app/models/Participant.scala
Expand Up @@ -16,11 +16,13 @@ case class Participant(
participantId: Long = 0,
username: String,
fullName: Option[String],
email: Option[String],
email: String,
password: Option[String],
isAdmin:Boolean=false,
isSuperUser:Boolean=false){

// require(Participant.ValidEmailAddress.findFirstIn(email).isDefined)

lazy val encryptedPassword = Participant.encrypt(password)

def createAndSaveEvent(eventName: String) = {
Expand All @@ -35,6 +37,8 @@ case class Participant(

object Participant {

val ValidEmailAddress = """^[0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$""".r

val DateFormat = new SimpleDateFormat("yyyy-MM-dd")

val authenticationMapper = {
Expand All @@ -49,7 +53,7 @@ object Participant {
get[Long]("participantid") ~
get[String]("username") ~
get[Option[String]]("fullname") ~
get[Option[String]]("email") ~
get[String]("email") ~
get[Boolean]("admin") ~
get[Boolean]("superuser") map {
case participantid~username~fullname~email~isadmin~issuperuser=> Participant( participantid, username, fullname, email, None, isadmin, issuperuser )
Expand Down

0 comments on commit aaeb7be

Please sign in to comment.