Use api to update and remove current permissions

This commit is contained in:
Everett Sochowski 2013-06-06 14:34:22 +02:00
parent b5364e8cf6
commit 73e43b196c
5 changed files with 272 additions and 9 deletions

View File

@ -60,6 +60,7 @@ import code.snippet.CommentsURLParams
import code.snippet.ManagementURLParams
import code.lib.ObpJson.OtherAccountsJson
import code.lib.ObpAPI
import code.snippet.PermissionsUrlParams
/**
* A class that's instantiated early and run. It allows the application
@ -148,7 +149,7 @@ class Boot extends Loggable{
}
}
def logAndReturnResult[T](result : Box[T]) : Box[T] = {
def logOrReturnResult[T](result : Box[T]) : Box[T] = {
result match {
case Failure(msg, _, _) => logger.info("Problem getting url " + tryo{S.uri} + ": " + msg)
case _ => //do nothing
@ -169,7 +170,7 @@ class Boot extends Loggable{
val transactionsURLParams = TransactionsListURLParams(bankId = bank, accountId = account, viewId = viewName)
val result = logAndReturnResult {
val result = logOrReturnResult {
for {
b <- BankAccount(bank, account) ?~ { "account " + account + " not found for bank " + bank }
@ -207,7 +208,7 @@ class Boot extends Loggable{
val urlParams = ManagementURLParams(bankUrl, accountUrl)
val result = logAndReturnResult {
val result = logOrReturnResult {
for {
otherAccountsJson <- ObpGet("/banks/" + bankUrl + "/accounts/" + accountUrl + "/owner/" + "other_accounts").flatMap(x => x.extractOpt[OtherAccountsJson])
} yield (otherAccountsJson, urlParams)
@ -240,7 +241,7 @@ class Boot extends Loggable{
val commentsURLParams = CommentsURLParams(bankId = bank, accountId = account, transactionId = transactionID, viewId = viewName)
val result = logAndReturnResult {
val result = logOrReturnResult {
for {
tJson <- transactionJson
} yield (tJson, commentsURLParams)
@ -257,16 +258,26 @@ class Boot extends Loggable{
case _ => calculateTransaction()
}
}
def getPermissions(URLParameters: List[String]): Box[(PermissionsJson, PermissionsUrlParams)] = {
if (URLParameters.length == 2) {
val bank = URLParameters(0)
val account = URLParameters(1)
logOrReturnResult {
for {
permissionsJson <- ObpGet("/banks/" + bank + "/accounts/" + account + "/users").flatMap(x => x.extractOpt[PermissionsJson])
} yield (permissionsJson, PermissionsUrlParams(bank, account))
}
} else Empty
}
// Build SiteMap
val sitemap = List(
Menu.i("Home") / "index",
Menu.i("OAuth Callback") / "oauthcallback" >> EarlyResponse(() => {
OAuthClient.handleCallback()
}),
Menu.i("Privilege Admin") / "admin" / "privilege" >> TestAccess(() => {
check(OBPUser.loggedIn_?)
}) >> LocGroup("admin")
submenus(Privilege.menus : _*),
Menu.i("Consumer Admin") / "admin" / "consumers" >> LocGroup("admin")
submenus(Consumer.menus : _*),
Menu("Consumer Registration", "Developers") / "consumer-registration",
@ -282,7 +293,18 @@ class Boot extends Loggable{
//test if the bank exists and if the user have access to management page
Menu.params[(OtherAccountsJson, ManagementURLParams)]("Management", "management", getAccount _ , t => List("")) / "banks" / * / "accounts" / * / "management",
Menu.params[(PermissionsJson, PermissionsUrlParams)]("Create Permission", "create permissions", getPermissions _ , x => List(""))
/ "permissions" / "banks" / * / "accounts" / * / "create" ,/*>> TestAccess(() => {
//TODO
Empty
}),*/
Menu.params[(PermissionsJson, PermissionsUrlParams)]("Permissions", "permissions", getPermissions _ , x => List("")) / "permissions" / "banks" / * / "accounts" / * ,/*>> TestAccess(() => {
//TODO
Empty
}),*/
Menu.params[(TransactionsJson, AccountJson, TransactionsListURLParams)]("Bank Account", "bank accounts", getTransactions _ , t => List("") )
/ "banks" / * / "accounts" / * / *,

View File

@ -77,6 +77,21 @@ object ObpAPI {
ObpPost(addCommentUrl, addCommentJson).flatMap(_.extractOpt[TransactionCommentJson])
}
def addPermission(bankId: String, accountId: String, userId : String, viewId: String) = {
val grantPermissionUrl = "/banks/" + bankId + "/accounts/" + accountId + "/users/" + userId + "/views/" + viewId
ObpPost(grantPermissionUrl, new JObject(Nil))
}
def removePermission(bankId: String, accountId: String, userId : String, viewId: String) = {
val removePermissionUrl = "/banks/" + bankId + "/accounts/" + accountId + "/users/" + userId + "/views/" + viewId
ObpDelete(removePermissionUrl)
}
def removeAllPermissions(bankId: String, accountId: String, userId: String) = {
val removeAllPermissionsUrl = "/banks/" + bankId + "/accounts/" + accountId + "/users/" + userId
ObpDelete(removeAllPermissionsUrl)
}
/**
* @return The jsons for the tags that were were successfully added
*/
@ -459,4 +474,8 @@ object ObpJson {
}
case class TransactionsJson(transactions: Option[List[TransactionJson]])
case class PermissionJson(user: Option[UserJson], views: Option[List[ViewJson]])
case class PermissionsJson(permissions : Option[List[PermissionJson]])
}

View File

@ -0,0 +1,120 @@
package code.snippet
import net.liftweb.http.S
import net.liftweb.util.Helpers._
import net.liftweb.http.SHtml
import net.liftweb.http.js.JE.JsRaw
import net.liftweb.common.Loggable
import net.liftweb.http.JsonHandler
import net.liftweb.json._
import net.liftweb.common.Full
import net.liftweb.common.Failure
import code.lib.Provider
import code.lib.OAuthClient
import net.liftweb.http.js.JsCmds.{Script, Noop}
import code.lib.ObpAPI
import code.lib.ObpJson._
import net.liftweb.util.CssSel
import net.liftweb.http.js.JsCmds.Replace
import scala.xml.NodeSeq
import net.liftweb.http.js.jquery.JqJsCmds.FadeOut
case class PermissionsUrlParams(bankId : String, accountId: String)
case class ClickJson(userId: String, checked: Boolean, viewId : String)
class PermissionManagement(params : (PermissionsJson, PermissionsUrlParams)) extends Loggable {
val permissionsJson = params._1
val urlParams = params._2
val NOOP_SELECTOR = "#i_am_an_id_that_should_never_exist" #> ""
implicit val formats = DefaultFormats
//"""test"'"''"test"""
def rowId(userId: String) = "permission_row_" + userId
val clickAjax = SHtml.ajaxCall(JsRaw("permissionsCheckBoxCallback(this)"), checkBoxClick)
val removeAjax = SHtml.ajaxCall(JsRaw("this.getAttribute('data-userid')"), userId => {
ObpAPI.removeAllPermissions(urlParams.bankId, urlParams.accountId, userId)
Noop
})
def checkBoxClick(rawData : String) = {
val data = tryo{parse(rawData).extract[ClickJson]}
data match {
case Full(d) => {
if(d.checked) ObpAPI.addPermission(urlParams.bankId, urlParams.accountId, d.userId, d.viewId)
else ObpAPI.removePermission(urlParams.bankId, urlParams.accountId, d.userId, d.viewId)
}
case Failure(msg, _, _) => logger.warn("Could not parse raw checkbox click data: " + rawData + ", " + msg)
case _ => logger.warn("Could not parse raw checkbox click data: " + rawData)
}
Noop
}
val checkBoxJsFunc = JsRaw("""
function permissionsCheckBoxCallback(checkbox) {
var json = {
"userId" : checkbox.getAttribute("data-userid"),
"checked" : checkbox.checked,
"viewId" : checkbox.getAttribute("data-viewid")
}
return JSON.stringify(json);
}
""").cmd
def checkBox(permission : PermissionJson, view : String, userId : String) = {
val onClick = "." + view + " [onclick]"
val userIdData = "." + view + " [data-userid]"
val viewIdData = "." + view + " [data-viewid]"
val permissionExists = (for {
views <- permission.views
} yield {
views.exists(_.id == (Some(view)))
}).getOrElse(false)
val checkedSelector : CssSel =
if(permissionExists) {{"." + view + " [checked]"} #> "checked"}
else NOOP_SELECTOR
checkedSelector &
onClick #> clickAjax &
userIdData #> userId &
viewIdData #> view
}
def manage = {
permissionsJson.permissions match {
case None => "* *" #> "No permissions exist"
case Some(ps) => {
".callback-script" #> Script(checkBoxJsFunc) &
".row" #> {
ps.map(permission => {
val userId = permission.user.flatMap(_.id).getOrElse("")
"* [id]" #> rowId(userId) &
".user *" #> permission.user.flatMap(_.display_name).getOrElse("") &
checkBox(permission, "owner", userId) &
checkBox(permission, "management", userId) &
checkBox(permission, "our-network", userId) &
checkBox(permission, "team", userId) &
checkBox(permission, "board", userId) &
checkBox(permission, "authorities", userId) &
".remove [data-userid]" #> userId &
".remove [onclick]" #> removeAjax
})
}
}
}
}
def createPermission = {
"* *" #> "Yo"
}
}

View File

@ -0,0 +1,66 @@
<!--
Open Bank Project - Transparency / Social Finance Web Application
Copyright (C) 2011, 2012, TESOBE / Music Pictures Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Email: contact@tesobe.com
TESOBE / Music Pictures Ltd
Osloerstrasse 16/17
Berlin 13359, Germany
This product includes software developed at
TESOBE (http://www.tesobe.com/)
by
Simon Redfern : simon AT tesobe DOT com
Stefan Bethge : stefan AT tesobe DOT com
Everett Sochowski : everett AT tesobe DOT com
Ayoub Benali: ayoub AT tesobe DOT com
-->
<div class="lift:surround?with=default;at=content">
<div class="lift:PermissionManagement.manage">
<div>Add Permission</div>
<div class="callback-script"></div>
<table>
<tr>
<th>User</th>
<th>Owner</th>
<th>Management</th>
<th>Our Network</th>
<th>Team</th>
<th>Board</th>
<th>Authorities</th>
<th></th><!-- "Remove" -->
</tr>
<tr class="row">
<td><span class="user">bob@example.com</span></td>
<td><input type="checkbox" class="owner"></td>
<td><input type="checkbox" class="management"></td>
<td><input type="checkbox" class="our-network"></td>
<td><input type="checkbox" class="team"></td>
<td><input type="checkbox" class="board"></td>
<td><input type="checkbox" class="authorities"></td>
<td><input class="remove" type="button" value="Remove"></td>
</tr>
</table>
<script type="text/javascript">
$('.remove').click(function () {
$(this).closest('.row').css('background-color', '#CC0000').fadeOut(1500, function() {$(this).remove();});
});
</script>
</div>
</div>

View File

@ -0,0 +1,36 @@
<!--
Open Bank Project - Transparency / Social Finance Web Application
Copyright (C) 2011, 2012, TESOBE / Music Pictures Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Email: contact@tesobe.com
TESOBE / Music Pictures Ltd
Osloerstrasse 16/17
Berlin 13359, Germany
This product includes software developed at
TESOBE (http://www.tesobe.com/)
by
Simon Redfern : simon AT tesobe DOT com
Stefan Bethge : stefan AT tesobe DOT com
Everett Sochowski : everett AT tesobe DOT com
Ayoub Benali: ayoub AT tesobe DOT com
-->
<div class="lift:surround?with=default;at=content">
<div class="lift:PermissionManagement.create">
<div>Add Permission</div>
</div>
</div>