Skip to content

Commit

Permalink
Fixes #15148: Add search/pagination to eventlogs UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Elaad committed Sep 20, 2019
1 parent 8d64073 commit a0ad2fa
Show file tree
Hide file tree
Showing 14 changed files with 2,479 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ trait EventLogRepository {
*/
def getEventLogByCriteria(criteria: Option[String], limit :Option[Int] = None, orderBy: Option[String] = None) : IOResult[Vector[EventLog]]

def count: IOResult[Long]

def getEventLogByChangeRequest(
changeRequest : ChangeRequestId
, xpath : String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ class EventLogJdbcRepository(
}).transact(xa))
}

def count: IOResult[Long] = {
val q ="SELECT count(*) FROM eventlog"
//sql"SELECT count(*) FROM eventlog".query[Long].option.transact(xa).unsafeRunSync
transactIOResult(s"Error when retrieving event logs count with request: ${q}")(xa => (for {
entries <- query[Long](q).unique
} yield {
entries
}).transact(xa))
}

def getEventLogByCriteria(criteria : Option[String], optLimit:Option[Int] = None, orderBy:Option[String]) : IOResult[Vector[EventLog]] = {

val where = criteria.map(c => s"where ${c}").getOrElse("")
Expand Down
5 changes: 5 additions & 0 deletions webapp/sources/rudder/rudder-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ along with Rudder. If not, see <http://www.gnu.org/licenses/>.
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring-security-version}</version>
</dependency>

<dependency>
<groupId>com.normation.rudder</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
*************************************************************************************
* Copyright 2019 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/

package com.normation.rudder.rest.internal
import com.normation.box._
import com.normation.eventlog._
import com.normation.rudder.db.Doobie
import com.normation.rudder.repository.EventLogRepository
import com.normation.rudder.rest.RestExtractorService
import com.normation.rudder.rest.RestUtils._
import com.normation.rudder.web.components.DateFormaterService
import com.normation.rudder.web.services._
import net.liftweb.common._
import net.liftweb.http.rest.RestHelper
import net.liftweb.http.{JsonResponse, LiftResponse, Req, S}
import net.liftweb.json.JValue
import net.liftweb.json.JsonDSL._

class EventLogAPI (
repos: EventLogRepository
, restExtractor : RestExtractorService
, eventLogDetail : EventListDisplayer
, doobie : Doobie
) extends RestHelper with Loggable {

def serialize(event: EventLog): JValue = {
import net.liftweb.json.JsonDSL._

( ("id" -> (event.id.map(_.toString).getOrElse("Unknown"): String))
~ ("date" -> DateFormaterService.getFormatedDate(event.creationDate))
~ ("actor" -> event.principal.name)
~ ("type" -> S.?("rudder.log.eventType.names." + event.eventType.serialize))
~ ("description" -> eventLogDetail.displayDescription(event).toString)
~ ("hasDetails" -> (if(event.details != <entry></entry>) true else false))
)
}

def responseFormater(draw: Int, totalRecord: Long, totalFiltered: Long, logs: Vector[EventLog], errorMsg: Option[String] = None): JValue = {
errorMsg match {
case Some(msg) =>
( ("draw" -> draw)
~ ("recordsTotal" -> totalRecord)
~ ("recordsFiltered" -> totalFiltered)
~ ("data" -> logs.map(serialize))
~ ("error" -> msg)
)
case _ =>
( ("draw" -> draw)
~ ("recordsTotal" -> totalRecord)
~ ("recordsFiltered" -> totalFiltered)
~ ("data" -> logs.map(serialize))
)
}
}

def getEventLogBySlice(start: Int, nbelement: Int, criteria : Option[String], optLimit:Option[Int] = None, orderBy:Option[String]): Box[(Int,Vector[EventLog])] = {
repos.getEventLogByCriteria(criteria, optLimit, orderBy).toBox match {
case Full(events) => Full((events.size, events.slice(start,start+nbelement)))
case eb: EmptyBox => eb ?~! s"Error when trying fetch eventlogs from database for page ${(start/nbelement)+1}"
}
}

def requestDispatch: PartialFunction[Req, () => Box[LiftResponse]] = {
case Get(Nil, req) =>

val draw = req.params.get("draw") match {
case Some(value :: Nil) => Full(value.toInt)
case None => Failure("Missing 'draw' field from datatable's request")
}
val start = req.params.get("start") match {
case Some(value :: Nil) => Full(value)
case None => Failure("Missing 'start' field from datatable's request")
}
val length = req.params.get("length") match {
case Some(value :: Nil) => Full(value)
case None => Failure("Missing 'length' field from datatable's request")
}

val response = (draw, start, length) match {
case (Full(d), Full(s), Full(l)) =>
repos.count.toBox match {
case Full(totalRecord) =>
getEventLogBySlice(s.toInt, l.toInt, None, None, Some("creationdate DESC" )) match {
case Full((totalFiltered, events)) =>
responseFormater(d, totalRecord, totalFiltered, events)
case eb: EmptyBox =>
val fail = eb ?~! "Failed to get eventlogs"
responseFormater(d, totalRecord, 0, Vector.empty, Some(fail.messageChain))
}
case eb: EmptyBox =>
val fail = eb ?~! "Failed to get event log's count"
responseFormater(d, 0, 0, Vector.empty, Some(fail.messageChain))
}

case (ebDraw: EmptyBox,_, _) =>
val fail = ebDraw ?~! "Missing parameter in request"
responseFormater(0, 0, 0, Vector.empty, Some(fail.messageChain))

case (_,ebStart: EmptyBox, _) =>
val fail = ebStart ?~! "Missing parameter in request"
responseFormater(0, 0, 0, Vector.empty, Some(fail.messageChain))

case (_,_, ebLength: EmptyBox) =>
val fail = ebLength ?~! "Missing parameter in request"
responseFormater(0, 0, 0, Vector.empty, Some(fail.messageChain))
}
JsonResponse(response, Nil, Nil, 200)

case Get(id :: "details" :: Nil, _) =>
repos.getEventLogByCriteria(Some(s"id = $id")).toBox match {
case Full(e) =>
e.headOption match {
case Some(eventLog) =>
val crid = eventLog.id.flatMap(repos.getEventLogWithChangeRequest(_).toBox match {
case Full(Some((_, crId))) => crId
case _ => None
})
val htmlDetails = eventLogDetail.displayDetails(eventLog, crid)
toJsonResponse(None, "content" -> htmlDetails.toString())("eventdetails", prettify = false)
case None =>
toJsonError(None, s"EventLog $id not found")("eventdetails", prettify = false)
}
case eb: EmptyBox =>
val e = eb ?~! s"Error when trying to retrieve eventlog : $id"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}
}
serve("secure" / "api" / "eventlog" prefix requestDispatch)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/

package com.normation.rudder.web.services

import com.normation.rudder.{AuthorizationType, Rights, RudderAccount, User}
import com.normation.rudder.api.ApiAuthorization
import net.liftweb.http.SessionVar
import org.springframework.security.core.context.SecurityContextHolder

/**
* An utility class that get the currently logged user
* (if any)
*
*/
object CurrentUserService extends SessionVar[Option[RudderUserDetail]] ({
SecurityContextHolder.getContext.getAuthentication match {
case null => None
case auth => auth.getPrincipal match {
case u:RudderUserDetail => Some(u)
case _ => None
}
}

}) with User {

def getRights : Rights = this.get match {
case Some(u) => u.authz
case None => new Rights(AuthorizationType.NoRights)
}

def account : RudderAccount = this.get match {
case None => RudderAccount.User("unknown", "")
case Some(u) => u.account
}

def checkRights(auth:AuthorizationType) : Boolean = {
val authz = getRights.authorizationTypes
if (authz.contains(AuthorizationType.NoRights)) false
else auth match{
case AuthorizationType.NoRights => false
case _ => authz.contains(auth)
}
}

def getApiAuthz: ApiAuthorization = {
this.get match {
case None => ApiAuthorization.None
case Some(u) => u.apiAuthz
}
}
}
Loading

0 comments on commit a0ad2fa

Please sign in to comment.