This commit is contained in:
shimamoto
2013-07-11 13:44:12 +09:00
17 changed files with 91 additions and 57 deletions

View File

@@ -70,7 +70,7 @@ trait AccountControllerBase extends AccountManagementControllerBase with FlashMa
val userName = params("userName")
getAccountByUserName(userName).map { account =>
updateAccount(account.copy(
password = form.password.map(encrypt).getOrElse(account.password),
password = form.password.map(sha1).getOrElse(account.password),
mailAddress = form.mailAddress,
url = form.url))
@@ -93,7 +93,7 @@ trait AccountControllerBase extends AccountManagementControllerBase with FlashMa
post("/register", newForm){ form =>
if(loadSystemSettings().allowAccountRegistration){
createAccount(form.userName, encrypt(form.password), form.mailAddress, false, form.url)
createAccount(form.userName, sha1(form.password), form.mailAddress, false, form.url)
updateImage(form.userName, form.fileId, false)
redirect("/signin")
} else NotFound

View File

@@ -95,7 +95,23 @@ abstract class ControllerBase extends ScalatraFilter
/**
* Context object for the current request.
*/
case class Context(path: String, loginAccount: Option[Account], currentUrl: String, request: HttpServletRequest)
case class Context(path: String, loginAccount: Option[Account], currentUrl: String, request: HttpServletRequest){
/**
* Get object from cache.
*
* If object has not been cached with the specified key then retrieves by given action.
* Cached object are available during a request.
*/
def cache[A](key: String)(action: => A): A = {
Option(request.getAttribute("cache." + key).asInstanceOf[A]).getOrElse {
val newObject = action
request.setAttribute("cache." + key, newObject)
newObject
}
}
}
/**
* Base trait for controllers which manages account information.

View File

@@ -213,11 +213,17 @@ trait RepositoryViewerControllerBase extends ControllerBase {
if(repository.commitCount == 0){
repo.html.guide(repository)
} else {
val revision = if(revstr.isEmpty) repository.repository.defaultBranch else revstr
JGitUtil.withGit(getRepositoryDir(repository.owner, repository.name)){ git =>
// get latest commit
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(revision))
// get specified commit
val (revCommit, revision) = try {
val revision = if(revstr.isEmpty) repository.repository.defaultBranch else revstr
(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(revision)), revision)
} catch {
case e: NullPointerException => {
val revision = repository.branchList.head
(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(revision)), revision)
}
}
// get files
val files = JGitUtil.getFileList(git, revision, path)
// process README.md
@@ -225,20 +231,10 @@ trait RepositoryViewerControllerBase extends ControllerBase {
new String(JGitUtil.getContent(Git.open(getRepositoryDir(repository.owner, repository.name)), file.id, true).get, "UTF-8")
}
repo.html.files(
// current branch
revision,
// repository
repository,
// current path
if(path == ".") Nil else path.split("/").toList,
// latest commit
new JGitUtil.CommitInfo(revCommit),
// file list
files,
// readme
readme
)
repo.html.files(revision, repository,
if(path == ".") Nil else path.split("/").toList, // current path
new JGitUtil.CommitInfo(revCommit), // latest commit
files, readme)
}
}
}

View File

@@ -25,7 +25,7 @@ trait SignInControllerBase extends ControllerBase { self: SystemSettingsService
post("/signin", form){ form =>
val account = getAccountByUserName(form.userName)
if(account.isEmpty || account.get.password != encrypt(form.password)){
if(account.isEmpty || account.get.password != sha1(form.password)){
redirect("/signin")
} else {
session.setAttribute("LOGIN_ACCOUNT", account.get)

View File

@@ -47,7 +47,7 @@ trait UserManagementControllerBase extends AccountManagementControllerBase {
})
post("/admin/users/_new", newForm)(adminOnly { form =>
createAccount(form.userName, encrypt(form.password), form.mailAddress, form.isAdmin, form.url)
createAccount(form.userName, sha1(form.password), form.mailAddress, form.isAdmin, form.url)
updateImage(form.userName, form.fileId, false)
redirect("/admin/users")
})
@@ -61,7 +61,7 @@ trait UserManagementControllerBase extends AccountManagementControllerBase {
val userName = params("userName")
getAccountByUserName(userName).map { account =>
updateAccount(getAccountByUserName(userName).get.copy(
password = form.password.map(encrypt).getOrElse(account.password),
password = form.password.map(sha1).getOrElse(account.password),
mailAddress = form.mailAddress,
isAdmin = form.isAdmin,
url = form.url))

View File

@@ -59,7 +59,7 @@ class BasicAuthenticationFilter extends Filter with RepositoryService with Accou
private def isWritableUser(username: String, password: String, repository: RepositoryService.RepositoryInfo): Boolean = {
getAccountByUserName(username).map { account =>
account.password == encrypt(password) && hasWritePermission(repository.owner, repository.name, Some(account))
account.password == sha1(password) && hasWritePermission(repository.owner, repository.name, Some(account))
} getOrElse false
}

View File

@@ -7,8 +7,8 @@ import scala.slick.driver.H2Driver.simple._
* Provides some usable implicit conversions.
*/
object Implicits {
implicit def extendsSeq[A](seq: Seq[A]) = new {
implicit class RichSeq[A](seq: Seq[A]) {
def splitWith(condition: (A, A) => Boolean): Seq[Seq[A]] = split(seq)(condition)
@@ -26,7 +26,7 @@ object Implicits {
}
// TODO Should this implicit conversion move to model.Functions?
implicit def extendsColumn(c1: Column[Boolean]) = new {
implicit class RichColumn(c1: Column[Boolean]){
def &&(c2: => Column[Boolean], guard: => Boolean): Column[Boolean] = if(guard) c1 && c2 else c1
}

View File

@@ -2,7 +2,7 @@ package util
object StringUtil {
def encrypt(value: String): String = {
def sha1(value: String): String = {
val md = java.security.MessageDigest.getInstance("SHA-1")
md.update(value.getBytes)
md.digest.map(b => "%02x".format(b)).mkString

View File

@@ -75,16 +75,14 @@ object helpers {
// convert commit id to link
.replaceAll("(^|\\W)([a-f0-9]{40})(\\W|$)", "$1<a href=\"%s/%s/%s/commit/$2\">$2</a>$3").format(context.path, repository.owner, repository.name))
/**
* Returns &lt;img&gt; which displays the avatar icon.
* Looks up Gravatar if avatar icon has not been configured in user settings.
*/
def avatar(userName: String, size: Int, tooltip: Boolean = false)(implicit context: app.Context): Html = {
val account = Option(context.request.getAttribute("cache.account." + userName).asInstanceOf[model.Account]).orElse {
new AccountService {}.getAccountByUserName(userName).map { account =>
context.request.setAttribute("cache.account." + userName, account)
account
}
val account = context.cache(s"account.${userName}"){
new AccountService {}.getAccountByUserName(userName)
}
val src = account.collect { case account if(account.image.isEmpty) =>
s"""http://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress)}?s=${size}"""
@@ -92,16 +90,16 @@ object helpers {
s"""${context.path}/${userName}/_avatar"""
}
if(tooltip){
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}:px" data-toggle="tooltip" title=${userName}/>""")
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" data-toggle="tooltip" title=${userName}/>""")
} else {
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}:px" />""")
Html(s"""<img src=${src} class="avatar" style="width: ${size}px; height: ${size}px;" />""")
}
}
/**
* Implicit conversion to add mkHtml() to Seq[Html].
*/
implicit def extendsHtmlSeq(seq: Seq[Html]) = new {
implicit class RichHtmlSeq(seq: Seq[Html]) {
def mkHtml(separator: String) = Html(seq.mkString(separator))
def mkHtml(separator: scala.xml.Elem) = Html(seq.mkString(separator.toString))
}