Conflicts:
	src/main/scala/app/SettingsController.scala
This commit is contained in:
takezoe
2013-06-05 01:49:46 +09:00
25 changed files with 366 additions and 48 deletions

View File

@@ -6,6 +6,7 @@ class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
context.mount(new IndexController, "/")
context.mount(new SignInController, "/*")
context.mount(new UsersController, "/*")
context.mount(new WikiController, "/*")
context.mount(new CreateRepositoryController, "/*")
context.mount(new RepositoryViewerController, "/*")

View File

@@ -18,11 +18,11 @@ class CreateRepositoryController extends CreateRepositoryControllerBase
trait CreateRepositoryControllerBase extends ControllerBase {
self: RepositoryService with WikiService with UsersOnlyAuthenticator =>
case class RepositoryCreationForm(name: String, description: String) // TODO Option?
case class RepositoryCreationForm(name: String, description: Option[String])
val form = mapping(
"name" -> trim(label("Repository name", text(required, maxlength(40), repository))),
"description" -> trim(label("Description" , text()))
"description" -> trim(label("Description" , optional(text())))
)(RepositoryCreationForm.apply)
/**
@@ -39,7 +39,7 @@ trait CreateRepositoryControllerBase extends ControllerBase {
val loginUserName = context.loginAccount.get.userName
// Insert to the database at first
createRepository(form.name, loginUserName, Some(form.description))
createRepository(form.name, loginUserName, form.description)
// Create the actual repository
val gitdir = getRepositoryDir(loginUserName, form.name)

View File

@@ -7,22 +7,35 @@ import jp.sf.amateras.scalatra.forms._
class SettingsController extends SettingsControllerBase
with RepositoryService with AccountService with OwnerOnlyAuthenticator
trait SettingsControllerBase extends ControllerBase {
self: RepositoryService with AccountService with OwnerOnlyAuthenticator =>
case class OptionsForm(description: Option[String], defaultBranch: String, repositoryType: Int)
val optionsForm = mapping(
"description" -> trim(label("Description" , optional(text()))),
"defaultBranch" -> trim(label("Default Branch" , text(required, maxlength(100)))),
"repositoryType" -> trim(label("Repository Type", number()))
)(OptionsForm.apply)
case class CollaboratorForm(userName: String)
val form = mapping(
val collaboratorForm = mapping(
"userName" -> trim(label("Username", text(required, collaborator)))
)(CollaboratorForm.apply)
/**
* Redirect to the Options page.
*/
get("/:owner/:repository/settings")(ownerOnly {
val owner = params("owner")
val repository = params("repository")
redirect("/%s/%s/settings/options".format(owner, repository))
})
/**
* Display the Options page.
*/
get("/:owner/:repository/settings/options")(ownerOnly {
val owner = params("owner")
val repository = params("repository")
@@ -30,6 +43,22 @@ trait SettingsControllerBase extends ControllerBase {
settings.html.options(getRepository(owner, repository, servletContext).get)
})
/**
* Save the repository options.
*/
post("/:owner/:repository/settings/options", optionsForm){ form =>
val owner = params("owner")
val repository = params("repository")
// save repository options
saveRepositoryOptions(owner, repository, form.description, form.defaultBranch, form.repositoryType)
redirect("%s/%s/settings/options".format(owner, repository))
}
/**
* Display the Collaborators page.
*/
get("/:owner/:repository/settings/collaborators")(ownerOnly {
val owner = params("owner")
val repository = params("repository")
@@ -37,13 +66,31 @@ trait SettingsControllerBase extends ControllerBase {
settings.html.collaborators(getCollaborators(owner, repository), getRepository(owner, repository, servletContext).get)
})
post("/:owner/:repository/settings/collaborators/_add", form)(ownerOnly { form =>
/**
* Add the collaborator.
*/
post("/:owner/:repository/settings/collaborators/add", collaboratorForm)(ownerOnly { form =>
val owner = params("owner")
val repository = params("repository")
addCollaborator(owner, repository, form.userName)
redirect("/%s/%s/settings/collaborators".format(owner, repository))
})
/**
* Add the collaborator.
*/
get("/:owner/:repository/settings/collaborators/remove")(ownerOnly {
val owner = params("owner")
val repository = params("repository")
val userName = params("name")
removeCollaborator(owner, repository, userName)
redirect("/%s/%s/settings/collaborators".format(owner, repository))
})
/**
* Provides Constraint to validate the collaborator name.
*/
def collaborator: Constraint = new Constraint(){
def validate(name: String, value: String): Option[String] = {
getAccountByUserName(value) match {

View File

@@ -24,6 +24,7 @@ trait SignInControllerBase extends ControllerBase { self: AccountService =>
redirect("/signin")
} else {
session.setAttribute("LOGIN_ACCOUNT", account.get)
updateLastLoginDate(account.get.userName)
redirect("/%s".format(account.get.userName))
}
}

View File

@@ -1,9 +1,76 @@
package app
class UsersController extends ControllerBase {
import model._
import service._
import jp.sf.amateras.scalatra.forms._
get("/"){
class UsersController extends UsersControllerBase with AccountService
trait UsersControllerBase extends ControllerBase { self: AccountService =>
// TODO ユーザ名の先頭に_は使えないようにする利用可能文字チェック
case class UserForm(userName: String, password: String, mailAddress: String, userType: Int, url: Option[String])
val newForm = mapping(
"userName" -> trim(label("Username" , text(required, maxlength(100), unique))),
"password" -> trim(label("Password" , text(required, maxlength(100)))),
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"userType" -> trim(label("User Type" , number())),
"url" -> trim(label("URL" , optional(text(maxlength(200)))))
)(UserForm.apply)
val editForm = mapping(
"userName" -> trim(label("Username" , text())),
"password" -> trim(label("Password" , text(required, maxlength(100)))),
"mailAddress" -> trim(label("Mail Address" , text(required, maxlength(100)))),
"userType" -> trim(label("User Type" , number())),
"url" -> trim(label("URL" , optional(text(maxlength(200)))))
)(UserForm.apply)
get("/admin/users"){
admin.html.userlist(getAllUsers())
}
get("/admin/users/_new"){
admin.html.useredit(None)
}
post("/admin/users/_new", newForm){ form =>
val currentDate = new java.sql.Date(System.currentTimeMillis)
createAccount(Account(
userName = form.userName,
password = form.password,
mailAddress = form.mailAddress,
userType = form.userType,
url = form.url,
registeredDate = currentDate,
updatedDate = currentDate,
lastLoginDate = None))
redirect("/admin/users")
}
get("/admin/users/:userName/_edit"){
val userName = params("userName")
admin.html.useredit(getAccountByUserName(userName))
}
post("/admin/users/:name/_edit", editForm){ form =>
val userName = params("userName")
val currentDate = new java.sql.Date(System.currentTimeMillis)
updateAccount(getAccountByUserName(userName).get.copy(
password = form.password,
mailAddress = form.mailAddress,
userType = form.userType,
url = form.url,
updatedDate = currentDate))
redirect("/admin/users")
}
def unique: Constraint = new Constraint(){
def validate(name: String, value: String): Option[String] =
getAccountByUserName(value).map { _ => "User already exists." }
}
}

View File

@@ -11,6 +11,7 @@ class WikiController extends WikiControllerBase
trait WikiControllerBase extends ControllerBase {
self: WikiService with RepositoryService with CollaboratorsOnlyAuthenticator =>
// TODO ユーザ名の先頭に_は使えないようにする
case class WikiPageEditForm(pageName: String, content: String, message: Option[String], currentPageName: String)
val newForm = mapping(
@@ -32,8 +33,8 @@ trait WikiControllerBase extends ControllerBase {
val repository = params("repository")
getWikiPage(owner, repository, "Home") match {
case Some(page) => wiki.html.wiki("Home", page, getRepository(owner, repository, servletContext).get)
case None => wiki.html.wikiedit("Home", None, getRepository(owner, repository, servletContext).get)
case Some(page) => wiki.html.wiki("Home", page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
case None => redirect("/%s/%s/wiki/Home/_edit".format(owner, repository))
}
}
@@ -43,8 +44,8 @@ trait WikiControllerBase extends ControllerBase {
val pageName = params("page")
getWikiPage(owner, repository, pageName) match {
case Some(page) => wiki.html.wiki(pageName, page, getRepository(owner, repository, servletContext).get)
case None => wiki.html.wikiedit(pageName, None, getRepository(owner, repository, servletContext).get)
case Some(page) => wiki.html.wiki(pageName, page, getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
case None => redirect("/%s/%s/wiki/%s/_edit".format(owner, repository, pageName)) // TODO URLEncode
}
}
@@ -132,7 +133,7 @@ trait WikiControllerBase extends ControllerBase {
val owner = params("owner")
val repository = params("repository")
wiki.html.wikipages(getWikiPageList(owner, repository), getRepository(owner, repository, servletContext).get)
wiki.html.wikipages(getWikiPageList(owner, repository), getRepository(owner, repository, servletContext).get, isWritable(owner, repository))
}
get("/:owner/:repository/wiki/_history"){
@@ -166,6 +167,15 @@ trait WikiControllerBase extends ControllerBase {
}
}
def isWritable(owner: String, repository: String): Boolean = {
context.loginAccount match {
case Some(a) if(a.userType == AccountService.Administrator) => true
case Some(a) if(a.userName == owner) => true
case Some(a) if(getCollaborators(owner, repository).contains(a.userName)) => true
case _ => false
}
}
def unique: Constraint = new Constraint(){
def validate(name: String, value: String): Option[String] = {
if(getWikiPageList(params("owner"), params("repository")).contains(value)){

View File

@@ -6,9 +6,30 @@ import Database.threadLocalSession
trait AccountService {
def getAccountByUserName(userName: String): Option[Account] =
def getAccountByUserName(userName: String): Option[Account] =
Query(Accounts) filter(_.userName is userName.bind) firstOption
def getAllUsers(): List[Account] = Query(Accounts) sortBy(_.userName) list
def createAccount(account: Account): Unit = Accounts.* insert account
def updateAccount(account: Account): Unit =
Query(Accounts)
.filter { a => a.userName is account.userName.bind }
.map { a => a.password ~ a.mailAddress ~ a.userType ~ a.url.? ~ a.registeredDate ~ a.updatedDate ~ a.lastLoginDate.? }
.update (
account.password,
account.mailAddress,
account.userType,
account.url,
account.registeredDate,
account.updatedDate,
account.lastLoginDate)
def updateLastLoginDate(userName: String): Unit =
Query(Accounts).filter(_.userName is userName.bind).map(_.lastLoginDate)
.update(new java.sql.Date(System.currentTimeMillis))
}
object AccountService {

View File

@@ -26,7 +26,7 @@ trait RepositoryService { self: AccountService =>
val currentDate = new java.sql.Date(System.currentTimeMillis)
Repositories.* insert
Repositories insert
Repository(
repositoryName = repositoryName,
userName = userName,
@@ -104,11 +104,23 @@ trait RepositoryService { self: AccountService =>
}
/**
* TODO Updates the last activity date of the repository.
* Updates the last activity date of the repository.
*/
def updateLastActivityDate(userName: String, repositoryName: String): Unit = {
}
def updateLastActivityDate(userName: String, repositoryName: String): Unit =
Query(Repositories)
.filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) }
.map { _.lastActivityDate }
.update (new java.sql.Date(System.currentTimeMillis))
/**
* Save repository options.
*/
def saveRepositoryOptions(userName: String, repositoryName: String,
description: Option[String], defaultBranch: String, repositoryType: Int): Unit =
Query(Repositories)
.filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) }
.map { r => r.description.? ~ r.defaultBranch ~ r.repositoryType ~ r.updatedDate }
.update (description, defaultBranch, repositoryType, new java.sql.Date(System.currentTimeMillis))
/**
* Add collaborator to the repository.
@@ -118,11 +130,31 @@ trait RepositoryService { self: AccountService =>
* @param collaboratorName the collaborator name
*/
def addCollaborator(userName: String, repositoryName: String, collaboratorName: String): Unit =
Collaborators.* insert(Collaborator(userName, repositoryName, collaboratorName))
Collaborators insert(Collaborator(userName, repositoryName, collaboratorName))
/**
* Remove collaborator from the repository.
*
* @param userName the user name of the repository owner
* @param repositoryName the repository name
* @param collaboratorName the collaborator name
*/
def removeCollaborator(userName: String, repositoryName: String, collaboratorName: String): Unit =
(Query(Collaborators) filter { c =>
(c.userName is userName.bind) && (c.repositoryName is repositoryName.bind) && (c.collaboratorName is collaboratorName.bind)
}).delete
/**
* Returns the list of collaborators name which is sorted with ascending order.
*
* @param userName the user name of the repository owner
* @param repositoryName the repository name
* @return the list of collaborators name
*/
def getCollaborators(userName: String, repositoryName: String): List[String] =
(Query(Collaborators) filter { collaborator =>
(collaborator.userName is userName.bind) && (collaborator.repositoryName is repositoryName.bind)
(Query(Collaborators) filter { c =>
(c.userName is userName.bind) && (c.repositoryName is repositoryName.bind)
} sortBy(_.collaboratorName) list) map(_.collaboratorName)
}

View File

@@ -2,6 +2,8 @@ package view
import java.util.Date
import java.text.SimpleDateFormat
import twirl.api.Html
import org.pegdown._
import org.pegdown.LinkRenderer.Rendering
import org.pegdown.ast.WikiLinkNode
@@ -19,9 +21,9 @@ object helpers {
def date(date: Date): String = new SimpleDateFormat("yyyy/MM/dd").format(date)
// TODO escape html tags using HtmlEscapeUtils (Commons Lang)
def format(value: String): twirl.api.Html = twirl.api.Html(
def format(value: String): Html = Html(
value.replaceAll(" ", "&nbsp;").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll("\n", "<br>"))
/**
* Converts the issue number and the commit id to the link.
*/
@@ -59,7 +61,8 @@ object helpers {
}
}
})
twirl.api.Html(html)
Html(html)
}
/**