Skip to content

Commit

Permalink
Replace AuthenticatedUser with DB User
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Dec 5, 2024
1 parent b85c8b6 commit 0058871
Show file tree
Hide file tree
Showing 35 changed files with 138 additions and 158 deletions.
4 changes: 2 additions & 2 deletions API/Controller/Account/Authenticated/ChangePassword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public sealed partial class AuthenticatedAccountController
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> ChangePassword(ChangePasswordRequest data)
{
if (!PasswordHashingUtils.VerifyPassword(data.OldPassword, CurrentUser.DbUser.PasswordHash).Verified)
if (!PasswordHashingUtils.VerifyPassword(data.OldPassword, CurrentUser.PasswordHash).Verified)
{
return Problem(AccountError.PasswordChangeInvalidPassword);
}

var result = await _accountService.ChangePassword(CurrentUser.DbUser.Id, data.NewPassword);
var result = await _accountService.ChangePassword(CurrentUser.Id, data.NewPassword);

return result.Match(success => Ok(),
notFound => throw new Exception("Unexpected result, apparently our current user does not exist..."));
Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Account/Authenticated/ChangeUsername.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public sealed partial class AuthenticatedAccountController
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status403Forbidden, MediaTypeNames.Application.ProblemJson)] // UsernameRecentlyChanged
public async Task<IActionResult> ChangeUsername(ChangeUsernameRequest data)
{
var result = await _accountService.ChangeUsername(CurrentUser.DbUser.Id, data.Username,
CurrentUser.DbUser.Rank.IsAllowed(RankType.Staff));
var result = await _accountService.ChangeUsername(CurrentUser.Id, data.Username,
CurrentUser.Rank.IsAllowed(RankType.Staff));

return result.Match<IActionResult>(
success => Ok(),
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Devices/DeviceOtaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IActionResult> GetOtaUpdateHistory([FromRoute] Guid deviceId,
{
// Check if user owns device or has a share
var deviceExistsAndYouHaveAccess = await _db.Devices.AnyAsync(x =>
x.Id == deviceId && x.Owner == CurrentUser.DbUser.Id);
x.Id == deviceId && x.Owner == CurrentUser.Id);
if (!deviceExistsAndYouHaveAccess) return Problem(DeviceError.DeviceNotFound);

return RespondSuccessLegacy(await otaService.GetUpdates(deviceId));
Expand Down
22 changes: 11 additions & 11 deletions API/Controller/Devices/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed partial class DevicesController
[MapToApiVersion("1")]
public async Task<IActionResult> ListDevices()
{
var devices = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id)
var devices = await _db.Devices.Where(x => x.Owner == CurrentUser.Id)
.Select(x => new Models.Response.ResponseDevice
{
Id = x.Id,
Expand All @@ -52,7 +52,7 @@ public async Task<IActionResult> GetDeviceById([FromRoute] Guid deviceId)
var hasAuthPerms = IsAllowed(PermissionType.Devices_Auth);


var device = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId)
var device = await _db.Devices.Where(x => x.Owner == CurrentUser.Id && x.Id == deviceId)
.Select(x => new Models.Response.ResponseDeviceWithToken
{
Id = x.Id,
Expand Down Expand Up @@ -80,14 +80,14 @@ public async Task<IActionResult> GetDeviceById([FromRoute] Guid deviceId)
[MapToApiVersion("1")]
public async Task<IActionResult> EditDevice([FromRoute] Guid deviceId, [FromBody] HubEditRequest body, [FromServices] IDeviceUpdateService updateService)
{
var device = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId)
var device = await _db.Devices.Where(x => x.Owner == CurrentUser.Id && x.Id == deviceId)
.FirstOrDefaultAsync();
if (device == null) return Problem(DeviceError.DeviceNotFound);

device.Name = body.Name;
await _db.SaveChangesAsync();

await updateService.UpdateDeviceForAllShared(CurrentUser.DbUser.Id, device.Id, DeviceUpdateType.Updated);
await updateService.UpdateDeviceForAllShared(CurrentUser.Id, device.Id, DeviceUpdateType.Updated);

return Ok();
}
Expand All @@ -106,7 +106,7 @@ public async Task<IActionResult> EditDevice([FromRoute] Guid deviceId, [FromBody
[MapToApiVersion("1")]
public async Task<IActionResult> RegenerateDeviceToken([FromRoute] Guid deviceId)
{
var device = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId)
var device = await _db.Devices.Where(x => x.Owner == CurrentUser.Id && x.Id == deviceId)
.FirstOrDefaultAsync();
if (device == null) return Problem(DeviceError.DeviceNotFound);

Expand Down Expand Up @@ -135,7 +135,7 @@ public async Task<IActionResult> RemoveDevice([FromRoute] Guid deviceId, [FromSe
var affected = await _db.Devices.Where(x => x.Id == deviceId).WhereIsUserOrAdmin(x => x.OwnerNavigation, CurrentUser).ExecuteDeleteAsync();
if (affected <= 0) return Problem(DeviceError.DeviceNotFound);

await updateService.UpdateDeviceForAllShared(CurrentUser.DbUser.Id, deviceId, DeviceUpdateType.Deleted);
await updateService.UpdateDeviceForAllShared(CurrentUser.Id, deviceId, DeviceUpdateType.Deleted);

return Ok();
}
Expand Down Expand Up @@ -168,14 +168,14 @@ public async Task<Guid> CreateDeviceV2([FromBody] HubCreateRequest data, [FromSe
var device = new Common.OpenShockDb.Device
{
Id = Guid.NewGuid(),
Owner = CurrentUser.DbUser.Id,
Owner = CurrentUser.Id,
Name = data.Name,
Token = CryptoUtils.RandomString(256)
};
_db.Devices.Add(device);
await _db.SaveChangesAsync();

await updateService.UpdateDevice(CurrentUser.DbUser.Id, device.Id, DeviceUpdateType.Created);
await updateService.UpdateDevice(CurrentUser.Id, device.Id, DeviceUpdateType.Created);

Response.StatusCode = (int)HttpStatusCode.Created;
return device.Id;
Expand All @@ -196,7 +196,7 @@ public async Task<IActionResult> GetPairCode([FromRoute] Guid deviceId)
{
var devicePairs = _redis.RedisCollection<DevicePair>();

var deviceExists = await _db.Devices.AnyAsync(x => x.Id == deviceId && x.Owner == CurrentUser.DbUser.Id);
var deviceExists = await _db.Devices.AnyAsync(x => x.Id == deviceId && x.Owner == CurrentUser.Id);
if (!deviceExists) Problem(DeviceError.DeviceNotFound);
// replace with unlink?
var existing = await devicePairs.FindByIdAsync(deviceId.ToString());
Expand Down Expand Up @@ -232,8 +232,8 @@ public async Task<IActionResult> GetLiveControlGatewayInfo([FromRoute] Guid devi
{
// Check if user owns device or has a share
var deviceExistsAndYouHaveAccess = await _db.Devices.AnyAsync(x =>
x.Id == deviceId && (x.Owner == CurrentUser.DbUser.Id || x.Shockers.Any(y => y.ShockerShares.Any(
z => z.SharedWith == CurrentUser.DbUser.Id))));
x.Id == deviceId && (x.Owner == CurrentUser.Id || x.Shockers.Any(y => y.ShockerShares.Any(
z => z.SharedWith == CurrentUser.Id))));
if (!deviceExistsAndYouHaveAccess) return Problem(DeviceError.DeviceNotFound);

// Check if device is online
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Devices/ShockersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed partial class DevicesController
[MapToApiVersion("1")]
public async Task<IActionResult> GetShockers([FromRoute] Guid deviceId)
{
var deviceExists = await _db.Devices.AnyAsync(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId);
var deviceExists = await _db.Devices.AnyAsync(x => x.Owner == CurrentUser.Id && x.Id == deviceId);
if (!deviceExists) return Problem(DeviceError.DeviceNotFound);
var shockers = await _db.Shockers.Where(x => x.Device == deviceId).Select(x => new ShockerResponse
{
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Sessions/ListSessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed partial class SessionsController
[ProducesResponseType<IEnumerable<LoginSessionResponse>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
public async Task<IEnumerable<LoginSessionResponse>> ListSessions()
{
var sessions = await _sessionService.ListSessionsByUserId(CurrentUser.DbUser.Id);
var sessions = await _sessionService.ListSessionsByUserId(CurrentUser.Id);

return sessions.Select(LoginSessionResponse.MapFrom);
}
Expand Down
8 changes: 4 additions & 4 deletions API/Controller/Shares/LinkShareCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ [FromServices] IDeviceUpdateService deviceUpdateService
Share = x, x.Shocker.DeviceNavigation.Owner, x.Shocker.Device
}).FirstOrDefaultAsync();
if (shareCode == null) return Problem(ShareCodeError.ShareCodeNotFound);
if (shareCode.Owner == CurrentUser.DbUser.Id) return Problem(ShareCodeError.CantLinkOwnShareCode);
if (await _db.ShockerShares.AnyAsync(x => x.ShockerId == shareCodeId && x.SharedWith == CurrentUser.DbUser.Id))
if (shareCode.Owner == CurrentUser.Id) return Problem(ShareCodeError.CantLinkOwnShareCode);
if (await _db.ShockerShares.AnyAsync(x => x.ShockerId == shareCodeId && x.SharedWith == CurrentUser.Id))
return Problem(ShareCodeError.ShockerAlreadyLinked);

_db.ShockerShares.Add(new ShockerShare
{
SharedWith = CurrentUser.DbUser.Id,
SharedWith = CurrentUser.Id,
ShockerId = shareCode.Share.ShockerId,
PermSound = shareCode.Share.PermSound,
PermVibrate = shareCode.Share.PermVibrate,
Expand All @@ -56,7 +56,7 @@ [FromServices] IDeviceUpdateService deviceUpdateService

if (await _db.SaveChangesAsync() <= 1) throw new Exception("Error while linking share code to your account");

await deviceUpdateService.UpdateDevice(shareCode.Owner, shareCode.Device, DeviceUpdateType.ShockerUpdated, CurrentUser.DbUser.Id);
await deviceUpdateService.UpdateDevice(shareCode.Owner, shareCode.Device, DeviceUpdateType.ShockerUpdated, CurrentUser.Id);

return RespondSuccessLegacySimple("Successfully linked share code");
}
Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Shares/Links/AddShocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public sealed partial class ShareLinksController
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status409Conflict, MediaTypeNames.Application.ProblemJson)] // ShockerAlreadyInShareLink
public async Task<IActionResult> AddShocker([FromRoute] Guid shareLinkId, [FromRoute] Guid shockerId)
{
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.DbUser.Id && x.Id == shareLinkId);
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.Id && x.Id == shareLinkId);
if (!exists) return Problem(ShareLinkError.ShareLinkNotFound);

var ownShocker =
await _db.Shockers.AnyAsync(x => x.Id == shockerId && x.DeviceNavigation.Owner == CurrentUser.DbUser.Id);
await _db.Shockers.AnyAsync(x => x.Id == shockerId && x.DeviceNavigation.Owner == CurrentUser.Id);
if (!ownShocker) return Problem(ShockerError.ShockerNotFound);

if (await _db.ShockerSharesLinksShockers.AnyAsync(x => x.ShareLinkId == shareLinkId && x.ShockerId == shockerId))
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shares/Links/CreateShareLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<IActionResult> CreateShareLink([FromBody] ShareLinkCreate body
var entity = new ShockerSharesLink
{
Id = Guid.NewGuid(),
Owner = CurrentUser.DbUser,
Owner = CurrentUser,
ExpiresOn = body.ExpiresOn == null ? null : DateTime.SpecifyKind(body.ExpiresOn.Value, DateTimeKind.Utc),
Name = body.Name
};
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shares/Links/EditShockerShareLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public sealed partial class ShareLinksController
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // ShareLinkNotFound, ShockerNotInShareLink
public async Task<IActionResult> EditShocker([FromRoute] Guid shareLinkId, [FromRoute] Guid shockerId, [FromBody] ShareLinkEditShocker body)
{
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.DbUser.Id && x.Id == shareLinkId);
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.Id && x.Id == shareLinkId);
if (!exists) return Problem(ShareLinkError.ShareLinkNotFound);

var shocker =
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shares/Links/ListShareLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed partial class ShareLinksController
[ProducesResponseType<BaseResponse<IEnumerable<ShareLinkResponse>>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
public async Task<IActionResult> List()
{
var ownShareLinks = await _db.ShockerSharesLinks.Where(x => x.OwnerId == CurrentUser.DbUser.Id)
var ownShareLinks = await _db.ShockerSharesLinks.Where(x => x.OwnerId == CurrentUser.Id)
.Select(x => ShareLinkResponse.GetFromEf(x)).ToListAsync();

return RespondSuccessLegacy(ownShareLinks);
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shares/Links/PauseShockerShareLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public sealed partial class ShareLinksController
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // ShareLinkNotFound, ShockerNotInShareLink
public async Task<IActionResult> PauseShocker([FromRoute] Guid shareLinkId, [FromRoute] Guid shockerId, [FromBody] PauseRequest body)
{
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.DbUser.Id && x.Id == shareLinkId);
var exists = await _db.ShockerSharesLinks.AnyAsync(x => x.OwnerId == CurrentUser.Id && x.Id == shareLinkId);
if (!exists) return Problem(ShareLinkError.ShareLinkNotFound);

var shocker =
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Shares/V2CreateShareRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public sealed partial class SharesController
[ApiVersion("2")]
public async Task<IActionResult> CreateShare([FromBody] CreateShareRequest data)
{
if (data.User == CurrentUser.DbUser.Id)
if (data.User == CurrentUser.Id)
{
return Problem(ShareError.ShareRequestCreateCannotShareWithSelf);
}

var providedShockerIds = data.Shockers.Select(x => x.Id).ToArray();
var belongsToUsFuture = _db.Shockers.AsNoTracking().Where(x =>
x.DeviceNavigation.Owner == CurrentUser.DbUser.Id && providedShockerIds.Contains(x.Id)).Select(x => x.Id).Future();
x.DeviceNavigation.Owner == CurrentUser.Id && providedShockerIds.Contains(x.Id)).Select(x => x.Id).Future();

if (data.User != null)
{
Expand All @@ -51,7 +51,7 @@ public async Task<IActionResult> CreateShare([FromBody] CreateShareRequest data)
var shareRequest = new ShareRequest
{
Id = Guid.NewGuid(),
Owner = CurrentUser.DbUser.Id,
Owner = CurrentUser.Id,
User = data.User
};
_db.ShareRequests.Add(shareRequest);
Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Shares/V2GetShares.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class SharesController
[ApiVersion("2")]
public async Task<IEnumerable<GenericIni>> GetSharesByUsers()
{
var sharedToUsers = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.Owner == CurrentUser.DbUser.Id)
var sharedToUsers = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.Owner == CurrentUser.Id)
.Select(x => new GenericIni
{
Id = x.SharedWithNavigation.Id,
Expand All @@ -34,7 +34,7 @@ public async Task<IEnumerable<GenericIni>> GetSharesByUsers()
[ApiVersion("2")]
public async Task<IActionResult> GetSharesToUser(Guid userId)
{
var sharedWithUser = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.Owner == CurrentUser.DbUser.Id && x.SharedWith == userId)
var sharedWithUser = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.Owner == CurrentUser.Id && x.SharedWith == userId)
.Select(x => new UserShareInfo
{
Id = x.Shocker.Id,
Expand Down
14 changes: 7 additions & 7 deletions API/Controller/Shares/V2Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed partial class SharesController
[ApiVersion("2")]
public async Task<IEnumerable<ShareRequestBaseItem>> GetOutstandingRequestsList()
{
var outstandingShares = await _db.ShareRequests.Where(x => x.Owner == CurrentUser.DbUser.Id)
var outstandingShares = await _db.ShareRequests.Where(x => x.Owner == CurrentUser.Id)
.Select(x => new ShareRequestBaseItem()
{
Id = x.Id,
Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task<IEnumerable<ShareRequestBaseItem>> GetOutstandingRequestsList(
[ApiVersion("2")]
public async Task<IEnumerable<ShareRequestBaseItem>> GetIncomingRequestsList()
{
var outstandingShares = await _db.ShareRequests.Where(x => x.User == CurrentUser.DbUser.Id)
var outstandingShares = await _db.ShareRequests.Where(x => x.User == CurrentUser.Id)
.Select(x => new ShareRequestBaseItem
{
Id = x.Id,
Expand Down Expand Up @@ -86,7 +86,7 @@ public async Task<IEnumerable<ShareRequestBaseItem>> GetIncomingRequestsList()
[ApiVersion("2")]
public async Task<IActionResult> GetRequest(Guid id)
{
var outstandingShare = await _db.ShareRequests.Where(x => x.Id == id && (x.Owner == CurrentUser.DbUser.Id || x.User == CurrentUser.DbUser.Id))
var outstandingShare = await _db.ShareRequests.Where(x => x.Id == id && (x.Owner == CurrentUser.Id || x.User == CurrentUser.Id))
.Select(x => new ShareRequestBaseDetails()
{
Id = x.Id,
Expand Down Expand Up @@ -135,7 +135,7 @@ public async Task<IActionResult> GetRequest(Guid id)
public async Task<IActionResult> DeleteRequest(Guid id)
{
var deletedShareRequest = await _db.ShareRequests
.Where(x => x.Id == id && x.Owner == CurrentUser.DbUser.Id).ExecuteDeleteAsync();
.Where(x => x.Id == id && x.Owner == CurrentUser.Id).ExecuteDeleteAsync();

if (deletedShareRequest <= 0) return Problem(ShareError.ShareRequestNotFound);

Expand All @@ -149,7 +149,7 @@ public async Task<IActionResult> DeleteRequest(Guid id)
public async Task<IActionResult> DenyRequest(Guid id)
{
var deletedShareRequest = await _db.ShareRequests
.Where(x => x.Id == id && x.User == CurrentUser.DbUser.Id).ExecuteDeleteAsync();
.Where(x => x.Id == id && x.User == CurrentUser.Id).ExecuteDeleteAsync();

if (deletedShareRequest <= 0) return Problem(ShareError.ShareRequestNotFound);

Expand All @@ -163,11 +163,11 @@ public async Task<IActionResult> DenyRequest(Guid id)
// public async Task<IActionResult> RedeemRequest(Guid id)
// {
// var shareRequest = await _db.ShareRequests
// .Where(x => x.Id == id && (x.User == null || x.User == CurrentUser.DbUser.Id)).Include(x => x.ShareRequestsShockers).FirstOrDefaultAsync();
// .Where(x => x.Id == id && (x.User == null || x.User == CurrentUser.Id)).Include(x => x.ShareRequestsShockers).FirstOrDefaultAsync();
//
// if (shareRequest == null) return Problem(ShareError.ShareRequestNotFound);
//
// var alreadySharedShockers = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.OwnerNavigation.Id == shareRequest.Owner && x.SharedWith == CurrentUser.DbUser.Id).Select(x => x.ShockerId).ToArrayAsync();
// var alreadySharedShockers = await _db.ShockerShares.Where(x => x.Shocker.DeviceNavigation.OwnerNavigation.Id == shareRequest.Owner && x.SharedWith == CurrentUser.Id).Select(x => x.ShockerId).ToArrayAsync();
//
// foreach (var shareRequestShareRequestsShocker in shareRequest.ShareRequestsShockers)
// {
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shockers/ControlLogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed partial class ShockerController
public async Task<IActionResult> GetShockerLogs([FromRoute] Guid shockerId, [FromQuery] uint offset = 0,
[FromQuery] [Range(1, 500)] uint limit = 100)
{
var exists = await _db.Shockers.AnyAsync(x => x.DeviceNavigation.Owner == CurrentUser.DbUser.Id && x.Id == shockerId);
var exists = await _db.Shockers.AnyAsync(x => x.DeviceNavigation.Owner == CurrentUser.Id && x.Id == shockerId);
if (!exists) return Problem(ShockerError.ShockerNotFound);

var logs = await _db.ShockerControlLogs.Where(x => x.ShockerId == shockerId)
Expand Down
Loading

0 comments on commit 0058871

Please sign in to comment.