All Downloads are FREE. Search and download functionalities are using the official Maven repository.

securesocial.core.services.UserService.scala Maven / Gradle / Ivy

/**
 * Copyright 2012-2014 Jorge Aliss (jaliss at gmail dot com) - twitter: @jaliss
 *
 * 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 securesocial.core.services

import play.api.i18n.Messages

import scala.concurrent.Future
import securesocial.core.{ PasswordInfo, BasicProfile }
import securesocial.core.providers.MailToken

trait UserService[U] {

  /**
   * Finds a SocialUser that matches the specified id
   *
   * @param providerId the provider id
   * @param userId the user id
   * @return an optional profile
   */
  def find(providerId: String, userId: String): Future[Option[BasicProfile]]

  def find(providerId: String, userId: String, messages: Messages): Future[Option[BasicProfile]] = {
    find(providerId, userId)
  }

  /**
   * Finds a profile by email and provider
   *
   * @param email - the user email
   * @param providerId - the provider id
   * @return an optional profile
   */
  def findByEmailAndProvider(email: String, providerId: String): Future[Option[BasicProfile]]

  /**
   * Saves a profile.  This method gets called when a user logs in, registers or changes his password.
   * This is your chance to save the user information in your backing store.
   *
   * @param profile the user profile
   * @param mode a mode that tells you why the save method was called
   */
  def save(profile: BasicProfile, mode: SaveMode): Future[U]

  def save(profile: BasicProfile, mode: SaveMode, messages: Messages): Future[U] = {
    save(profile, mode)
  }

  /**
   * Links the current user to another profile
   *
   * @param current The current user instance
   * @param to the profile that needs to be linked to
   */
  def link(current: U, to: BasicProfile): Future[U]

  def link(current: U, to: BasicProfile, messages: Messages): Future[U] = {
    link(current, to)
  }

  /**
   * Returns an optional PasswordInfo instance for a given user
   *
   * @param user a user instance
   * @return returns an optional PasswordInfo
   */
  def passwordInfoFor(user: U): Future[Option[PasswordInfo]]

  /**
   * Updates the PasswordInfo for a given user
   *
   * @param user a user instance
   * @param info the password info
   * @return
   */
  def updatePasswordInfo(user: U, info: PasswordInfo): Future[Option[BasicProfile]]

  /**
   * Saves a mail token.  This is needed for users that
   * are creating an account in the system or trying to reset a password
   *
   * Note: If you do not plan to use the UsernamePassword provider just provide en empty
   * implementation
   *
   * @param token The token to save
   */
  def saveToken(token: MailToken): Future[MailToken]

  /**
   * Finds a token
   *
   * Note: If you do not plan to use the UsernamePassword provider just provide en empty
   * implementation
   *
   * @param token the token id
   * @return
   */
  def findToken(token: String): Future[Option[MailToken]]

  /**
   * Deletes a token
   *
   * Note: If you do not plan to use the UsernamePassword provider just provide en empty
   * implementation
   *
   * @param uuid the token id
   */
  def deleteToken(uuid: String): Future[Option[MailToken]]

  /**
   * Deletes all expired tokens
   *
   * Note: If you do not plan to use the UsernamePassword provider just provide en empty
   * implementation
   *
   */
  def deleteExpiredTokens()
}

/**
 * Save modes
 */
case class SaveMode(name: String) {
  def is(m: SaveMode): Boolean = this == m
}

object SaveMode {
  val LoggedIn = SaveMode("loggedIn")
  val SignUp = SaveMode("signUp")
  val PasswordChange = SaveMode("passwordChange")

  private val values = List(LoggedIn, SignUp, PasswordChange)

  def getFromString(str: String): Option[SaveMode] = {
    values.filter(_.name == str).headOption
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy