Skip to content

PDJB-1275: Part 2: Fix other assumptions a landlord has a base user ID - #1654

Open
samyou-softwire wants to merge 15 commits into
mainfrom
feat/PDJB-1275-fix-landlord-permission-checks
Open

PDJB-1275: Part 2: Fix other assumptions a landlord has a base user ID#1654
samyou-softwire wants to merge 15 commits into
mainfrom
feat/PDJB-1275-fix-landlord-permission-checks

Conversation

@samyou-softwire

@samyou-softwire samyou-softwire commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Ticket number

PDJB-1275

Goal of change

remove all general assumptions from the codebase that a landlord has a single base user ID

Description of main change(s)

fixes many controllers & services to use the general methods for getting the current landlord, and uses the landlord to compare base user IDs

Anything you'd like to highlight to the reviewer?

this does not include any changes in function around org landlords. all of these changes should be stuff that would be expected to 'just work' with org landlords. any cases where we may want function to change (emails, page copy etc) are covered by specific tickets, referenced in TODO

most of the changes in this PR are uninteresting, removing the principal param from controllers etc. of particular note are the services which will now base their logic off of landlords directly

Checklist

  • Test suite has been run in full locally and is passing
  • Branch has been rebased onto main and run locally, with everything working as expected (both for your new feature
    and any related functionality) not currently based on main, but does work locally right now
  • TODO comments referencing this JIRA ticket have been searched for and removed - if a future PR will address them,
    mention that here
  • QA instructions have been added to the ticket (particularly if this is the last PR required to complete the ticket) I don't think there's much to QA here. I presume we'll do a healthy amount of mobbing before release
  • This feature is not behind a feature flag. I've checked that this is appropriate and we're happy with this code becoming live as soon as we release

@samyou-softwire samyou-softwire self-assigned this Jul 28, 2026
@samyou-softwire
samyou-softwire changed the base branch from main to feat/PDJB-1275-add-service-methods-and-fix-site-globally-for-OL July 28, 2026 09:43
import org.springframework.data.jpa.repository.JpaRepository
import uk.gov.communities.prsdb.webapp.database.entity.OrganisationLandlordUser

// TODO: PDJB-1275: Move org landlord access checking to use this table

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now do

) {
fun isAddressOwned(uprn: Long): Boolean = propertyOwnershipRepository.existsByIsActiveTrueAndAddress_Uprn(uprn)

fun isAddressOwnedByUser(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was unused

@samyou-softwire
samyou-softwire force-pushed the feat/PDJB-1275-add-service-methods-and-fix-site-globally-for-OL branch from ffe9a28 to 5db182d Compare July 28, 2026 15:55
@samyou-softwire
samyou-softwire force-pushed the feat/PDJB-1275-fix-landlord-permission-checks branch from 0163c8e to 8469ec7 Compare July 28, 2026 16:28
@samyou-softwire
samyou-softwire force-pushed the feat/PDJB-1275-add-service-methods-and-fix-site-globally-for-OL branch from 5db182d to 0b68d23 Compare July 29, 2026 08:27
Base automatically changed from feat/PDJB-1275-add-service-methods-and-fix-site-globally-for-OL to main July 29, 2026 10:29
@samyou-softwire
samyou-softwire force-pushed the feat/PDJB-1275-fix-landlord-permission-checks branch 8 times, most recently from 58de050 to d4d0ffa Compare July 30, 2026 15:38
@samyou-softwire
samyou-softwire force-pushed the feat/PDJB-1275-fix-landlord-permission-checks branch from d4d0ffa to 537dbf6 Compare July 30, 2026 15:55
@samyou-softwire
samyou-softwire marked this pull request as ready for review July 31, 2026 08:23

@Travis-Softwire Travis-Softwire left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a monster! I've left some comments but nothing blocking, happy for it to be merged once they've been addressed

)
}

if (landlordService.retrieveLandlordByBaseUserId(principal.name) != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we've just removed this check altogether? Should we not instead check if the current user is associated with a landlord? This still feels like a failure mode worth checking

@RequestMapping(LANDLORD_BASE_URL, "/")
class LandlordController(
private val landlordService: LandlordService,
private val userToLandlordService: UserToLandlordService,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but the heavy use of this has got me thinking about whether we should cache the landlord ID in session on login, which would not only save at least one DB roundtrip per call for most LL endpoints, but would also potentially make our system operator functionality MUCH simpler to implement when we get there.

throw ResponseStatusException(
HttpStatus.NOT_FOUND,
"User $baseUserId is not authorized to update property ownership $propertyOwnershipId",
"Current user is not authorized to update property ownership $propertyOwnershipId",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a loss of fidelity in the logs that I don't think is necessary - we can still grab the base user Id to tell us who was trying to access something they shouldn't have been

private val updateConfirmationSender: EmailNotificationService<LandlordUpdateConfirmation>,
private val absoluteUrlProvider: AbsoluteUrlProvider,
) {
fun retrieveLandlordByRegNum(regNum: RegistrationNumberDataModel): Landlord? {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were we not using this? I thought we let LCs search by LRN?

fun getPropertyOwnershipIfCurrentUserAuthorized(propertyOwnershipId: Long): PropertyOwnership {
val propertyOwnership = getPropertyOwnership(propertyOwnershipId)
val baseUserId = SecurityContextHolder.getContext().authentication.name
val landlord = userToLandlordService.getLandlordForBaseUserIdOrNull(baseUserId)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this vs the getCurrentLandlordForUser you've used everywhere else?

@@ -89,8 +85,9 @@ class LandlordController(
@RequestParam(value = "page", required = false) @Min(1) page: Int = 1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we still using the principal param?

@@ -80,11 +80,9 @@ class InviteJointLandlordController(
@PathVariable invitationId: Long,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still using the principal param?

@@ -96,22 +94,19 @@ class InviteJointLandlordController(
principal: Principal,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still using the principal param?

whenever(mockPropertyOwnershipService.getPropertyOwnership(propertyId)).thenReturn(propertyOwnership)
setMockPrincipal(baseUserId)
whenever(mockLandlordService.retrieveLandlordByBaseUserId(baseUserId)).thenReturn(null)
whenever(mockUserToLandlordService.getCurrentLandlordForUser()).thenThrow(PrsdbWebException("Landlord not found"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test actually testing anything now or just asserting on the mock directly?

2,
3,
"USER_ID",
1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful here that we're using the correct arg in the correct place - might be worth using named arguments just in case these args change again in the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants