<commit_before>package controllers

import java.security.MessageDigest

import com.google.inject.Inject
import com.typesafe.scalalogging.StrictLogging
import controllers.UserController._
import dal.UserRepository
import play.api.data.Forms._
import play.api.data.{Form, FormError}
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.Json
import play.api.mvc._
import sun.misc.BASE64Encoder

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

class UserController @Inject()(repo: UserRepository, val messagesApi: MessagesApi)(implicit ec: ExecutionContext) extends Controller with I18nSupport with StrictLogging {

  val registerForm: Form[RegisterForm] = Form {
    mapping(
      "name" -> nonEmptyText,
      "password" -> nonEmptyText,
      "verify" -> nonEmptyText
    )(RegisterForm.apply)(RegisterForm.unapply) verifying(passwordsNotMatched, validatePassword _)
  }

  val loginForm: Form[LoginForm] = Form {
    mapping(
      "name" -> nonEmptyText,
      "password" -> nonEmptyText
    )(LoginForm.apply)(LoginForm.unapply)
  }

  def getLogin = Action { implicit request =>
    request.session.get(username).fold(Ok(views.html.login(loginForm))) { name =>
      Redirect(routes.Application.index)
        .flashing(flashToUser -> s"$name $loggedIn")
    }
  }

  def getRegister = Action { implicit request =>
    request.session.get(username).fold(Ok(views.html.register(registerForm))) { _ =>
      Redirect(routes.Application.index)
        .flashing(flashToUser -> alreadyRegistered)
    }
  }

  def postLogin() = Action.async { implicit request =>
    def wrongPassword = BadRequest(
      views.html.login(loginForm.bindFromRequest
        .withError("password", passwordNot<PASSWORD>TheName)
      )
    )

    loginForm.bindFromRequest.fold(
      errorForm => {
        Future.successful(BadRequest(views.html.login(errorForm)))
      },
      user => {
        repo.passwordFor(user.name).map {
          _.fold(wrongPassword) { hashedAndSalted =>
            val salt = hashedAndSalted.split(",")(1)
            if (hashedAndSalted != passwordHash(user.password, salt)) wrongPassword
            else Redirect(routes.Application.index)
              .flashing(flashToUser -> s"${user.name} $loggedIn")
              .withSession(username -> user.name)
          }
        }.recover {
          case e =>
            logger.error(e.getMessage, e)
            Ok(views.html.login(loginForm.bindFromRequest
              .withError("password", errorDuringPasswordCheck)))
        }
      }
    )
  }

  def postRegister() = Action.async { implicit request =>
    registerForm.bindFromRequest.fold(
      errorForm => {
        Future.successful(BadRequest(views.html.register(withPasswordMatchError(errorForm))))
      },
      user => {
        val hash = passwordHash(user.password, Random.nextInt().toString)
        repo.create(user.name, hash).map { p =>
          Redirect(routes.Application.index)
            .withSession(username -> p.name)
            .flashing(flashToUser -> userRegistered)
        }.recover {
          case e => logger.error(e.getMessage, e)
            Ok(views.html.register(registerForm.bindFromRequest
              .withError("name", nameRegistered)))
        }
      }
    )
  }<commit_msg>register: change duplicate user to warn<commit_after>package controllers

import java.security.MessageDigest

import com.google.inject.Inject
import com.typesafe.scalalogging.StrictLogging
import controllers.UserController._
import dal.UserRepository
import play.api.data.Forms._
import play.api.data.{Form, FormError}
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.Json
import play.api.mvc._
import sun.misc.BASE64Encoder

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

class UserController @Inject()(repo: UserRepository, val messagesApi: MessagesApi)(implicit ec: ExecutionContext) extends Controller with I18nSupport with StrictLogging {

  val registerForm: Form[RegisterForm] = Form {
    mapping(
      "name" -> nonEmptyText,
      "password" -> nonEmptyText,
      "verify" -> nonEmptyText
    )(RegisterForm.apply)(RegisterForm.unapply) verifying(passwordsNotMatched, validatePassword _)
  }

  val loginForm: Form[LoginForm] = Form {
    mapping(
      "name" -> nonEmptyText,
      "password" -> nonEmptyText
    )(LoginForm.apply)(LoginForm.unapply)
  }

  def getLogin = Action { implicit request =>
    request.session.get(username).fold(Ok(views.html.login(loginForm))) { name =>
      Redirect(routes.Application.index)
        .flashing(flashToUser -> s"$name $loggedIn")
    }
  }

  def getRegister = Action { implicit request =>
    request.session.get(username).fold(Ok(views.html.register(registerForm))) { _ =>
      Redirect(routes.Application.index)
        .flashing(flashToUser -> alreadyRegistered)
    }
  }

  def postLogin() = Action.async { implicit request =>
    def wrongPassword = BadRequest(
      views.html.login(loginForm.bindFromRequest
        .withError("password", <PASSWORD>)
      )
    )

    loginForm.bindFromRequest.fold(
      errorForm => {
        Future.successful(BadRequest(views.html.login(errorForm)))
      },
      user => {
        repo.passwordFor(user.name).map {
          _.fold(wrongPassword) { hashedAndSalted =>
            val salt = hashedAndSalted.split(",")(1)
            if (hashedAndSalted != passwordHash(user.password, salt)) wrongPassword
            else Redirect(routes.Application.index)
              .flashing(flashToUser -> s"${user.name} $loggedIn")
              .withSession(username -> user.name)
          }
        }.recover {
          case e =>
            logger.error(e.getMessage, e)
            Ok(views.html.login(loginForm.bindFromRequest
              .withError("password", errorDuringPasswordCheck)))
        }
      }
    )
  }

  def postRegister() = Action.async { implicit request =>
    registerForm.bindFromRequest.fold(
      errorForm => {
        Future.successful(BadRequest(views.html.register(withPasswordMatchError(errorForm))))
      },
      user => {
        val hash = passwordHash(user.password, Random.nextInt().toString)
        repo.create(user.name, hash).map { p =>
          Redirect(routes.Application.index)
            .withSession(username -> p.name)
            .flashing(flashToUser -> userRegistered)
        }.recover {
          case e => logger.warn(e.getMessage, e)
            Ok(views.html.register(registerForm.bindFromRequest
              .withError("name", nameRegistered)))
        }
      }
    )
  }
{'fraction_non_alphanumeric': 0.1180839775655601, 'fraction_numerical': 0.002728512960436562, 'mean_word_length': 2.9041420118343195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1073926', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1904, 'n_words': 325}