Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
clarktsiory committed Feb 19, 2024
1 parent 2d95f97 commit 8ce9dc8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ case class UserSession(
creationDate: DateTime,
authMethod: String,
permissions: List[String],
tenants: String,
tenants: String, // TODO: once https://github.com/Normation/rudder/pull/5391 is merged, change to List[TenantId]
endDate: Option[DateTime],
endCause: Option[String]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ class InMemoryUserRepository(userBase: Ref[Map[String, UserInfo]], sessionBase:
* endCause text
*/
class JdbcUserRepository(doobie: Doobie) extends UserRepository {
import JdbcUserRepository._
import com.normation.rudder.db.Doobie.DateTimeMeta
import com.normation.rudder.db.json.implicits._
import com.normation.rudder.users.UserSerialization._
Expand Down Expand Up @@ -603,7 +604,7 @@ class JdbcUserRepository(doobie: Doobie) extends UserRepository {

override def getLastPreviousLogin(userId: String): IOResult[Option[UserSession]] = {
val sql =
sql"""select * from usersessions where userid = ${userId} and enddate is not null order by creationdate desc limit 1"""
sql"""select ${userSessionsAll} from usersessions where userid = ${userId} and enddate is not null order by creationdate desc limit 1"""

transactIOResult(s"Error when retrieving information for previous session for '${userId}'")(xa =>
sql.query[UserSession].option.transact(xa)
Expand Down Expand Up @@ -846,3 +847,7 @@ class JdbcUserRepository(doobie: Doobie) extends UserRepository {
}
}
}

object JdbcUserRepository {
val userSessionsAll: Fragment = fr"userid, sessionid, creationdate, authmethod, permissions, tenants, enddate, endcause"
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ import zio.interop.catz._
* During 7.3 cycle, we added the registration of users and their sessions in base.
* This is to allows better security logs on user sessions + allows to de-correlate rudder
* users from the `rudder-user.xml` file.
* In 8.1 we add the tenants column to the UserSessions table.
*/
class CheckTableUsers(
doobie: Doobie
) extends BootstrapChecks {

import doobie._

override def description: String = "Check if database tables Users and UserSessions exist"
override def description: String =
"Check if database tables Users and UserSessions exist and tenants column is present in UserSessions table."

def createUserTables: IOResult[Unit] = {
val sql1 = sql"""CREATE TABLE IF NOT EXISTS Users (
Expand Down Expand Up @@ -85,10 +87,18 @@ class CheckTableUsers(
transactIOResult(s"Error with 'UserSessions' table creation")(xa => sql2.update.run.transact(xa)).unit
}

def addTenantsColumn: IOResult[Unit] = {
val sql = sql"""ALTER TABLE UserSessions ADD COLUMN IF NOT EXISTS tenants text;
UPDATE UserSessions SET tenants = '' WHERE tenants IS NULL;
"""
transactIOResult(s"Error with 'UserSessions' table adding 'tenants' column")(xa => sql.update.run.transact(xa)).unit
}

override def checks(): Unit = {
val prog = {
for {
_ <- createUserTables
_ <- addTenantsColumn
} yield ()
}

Expand Down

0 comments on commit 8ce9dc8

Please sign in to comment.