Skip to content

Commit

Permalink
Change from if/else to match/case statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ningyougang committed May 28, 2021
1 parent 51ca755 commit 92b2a38
Showing 1 changed file with 119 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,137 +264,139 @@ protected[core] abstract class EntitlementProvider(
entityName: FullyQualifiedEntityName,
get: (ArtifactStore[WhiskEntity], DocId, DocRevision, Boolean) => Future[WhiskAction],
permissions: Option[String] = None)(implicit transid: TransactionId): Future[Unit] = {
if (operation == "create") {
permissions
.map { value =>
if (WhiskAction.permissionList.contains(value)) {
Future.successful(())
} else {
val errorInfo =
s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
operation match {
case "create" =>
permissions
.map { value =>
if (WhiskAction.permissionList.contains(value)) {
Future.successful(())
} else {
val errorInfo =
s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
}
}
}
.getOrElse(Future.successful(()))
} else if (operation == "update") {
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

val errorInfo = s"have no permission to ${operation} this action"
permissions match {
case Some(value) =>
if (!WhiskAction.permissionList.contains(value)) {
val errorInfo =
s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
val passedUpdatePermission = value.charAt(1)
if (passedUpdatePermission == 'w') { // make it to modifiable
Future.successful(())
.getOrElse(Future.successful(()))
case "update" =>
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

val errorInfo = s"have no permission to ${operation} this action"
permissions match {
case Some(value) =>
if (!WhiskAction.permissionList.contains(value)) {
val errorInfo =
s"give error permission code: ${value}, available permission is in ${WhiskAction.permissionList}"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
val passedUpdatePermission = value.charAt(1)
if (passedUpdatePermission == 'w') { // make it to modifiable
Future.successful(())
} else {
val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
}
}
}
case None =>
val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
case None =>
val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
}
}
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
} else if (operation == "remove") {
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
val errorInfo = s"have no permission to ${operation} this action"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
} else if (operation == "invoke") {
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

// the user who is owner by default
var currentExecutePermission = currentPermissions.charAt(2)
var errorInfo = s"have no permission to ${operation} this action"
if (user.namespace.name.asString != entityName.path.root.asString) { // the user who invoke the shared action
currentExecutePermission = currentPermissions.charAt(5)
errorInfo = s"have no permission to ${operation} this shared action"
case "remove" =>
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

val currentUpdatePermission = currentPermissions.charAt(1)
if (currentUpdatePermission == '-') {
val errorInfo = s"have no permission to ${operation} this action"
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
}
if (currentExecutePermission == '-') { //have no permission
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
} else { // download the code
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

if (user.namespace.name.asString != entityName.path.root.asString) { // the shared user who download the action
val errorInfo = s"have no permission to download this shared action"
val downloadPermissionOfSharedUser = currentPermissions.charAt(3)
if (downloadPermissionOfSharedUser == '-') {
case "invoke" =>
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

// the user who is owner by default
var currentExecutePermission = currentPermissions.charAt(2)
var errorInfo = s"have no permission to ${operation} this action"
if (user.namespace.name.asString != entityName.path.root.asString) { // the user who invoke the shared action
currentExecutePermission = currentPermissions.charAt(5)
errorInfo = s"have no permission to ${operation} this shared action"
}
if (currentExecutePermission == '-') { //have no permission
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
} else {
// the owner has download permission on any situation
Future.successful(())
}
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
case _ =>
// download the code
get(entityStore, entityName.toDocId, DocRevision.empty, true)
.flatMap { whiskAction =>
val currentPermissions = whiskAction.annotations
.get(WhiskAction.permissionsFieldName)
.map(value => value.convertTo[String])
.getOrElse(WhiskAction.defaultPermissions)

if (user.namespace.name.asString != entityName.path.root.asString) { // the shared user who download the action
val errorInfo = s"have no permission to download this shared action"
val downloadPermissionOfSharedUser = currentPermissions.charAt(3)
if (downloadPermissionOfSharedUser == '-') {
Future.failed(RejectRequest(Forbidden, Some(ErrorResponse(errorInfo, transid))))
} else {
Future.successful(())
}
} else {
// the owner has download permission on any situation
Future.successful(())
}
}
.recoverWith {
case t: RejectRequest =>
Future.failed(t)
case _ =>
Future.successful(())
}
}
}

Expand Down

0 comments on commit 92b2a38

Please sign in to comment.