Skip to content

Commit

Permalink
Create alerts button and frame.
Browse files Browse the repository at this point in the history
Create cards for alerts.
  • Loading branch information
bitbound committed Mar 26, 2020
1 parent bfc84be commit 869fe9a
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 27 deletions.
2 changes: 0 additions & 2 deletions Server/API/AlertsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public AlertsController(DataService dataService, IEmailSenderEx emailSender)
private IEmailSenderEx EmailSender { get; }

[HttpPost("Create")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public async Task<IActionResult> Create(AlertOptions alertOptions)
{
Request.Headers.TryGetValue("OrganizationID", out var orgID);
Expand Down Expand Up @@ -94,7 +93,6 @@ await EmailSender.SendEmailAsync(alertOptions.EmailTo,
}

[HttpPost("Delete/{alertID}")]
[ServiceFilter(typeof(ApiAuthorizationFilter))]
public async Task<IActionResult> Delete(string alertID)
{
Request.Headers.TryGetValue("OrganizationID", out var orgID);
Expand Down
6 changes: 6 additions & 0 deletions Server/Auth/ApiAuthorizationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void OnAuthorization(AuthorizationFilterContext context)
if (DataService.ValidateApiToken(apiToken, apiSecret, context.HttpContext.Request.Path, context.HttpContext.Connection.RemoteIpAddress.ToString()))
{
var orgID = DataService.GetApiToken(apiToken)?.OrganizationID;

// In case the filter gets run twice.
if (context.HttpContext.Request.Headers.ContainsKey("OrganizationID"))
{
return;
}
context.HttpContext.Request.Headers.Add("OrganizationID", orgID);
return;
}
Expand Down
6 changes: 6 additions & 0 deletions Server/Pages/EditDevice.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ else
<input type="text" class="form-control" name="device-name" readonly value="@Model.DeviceName" />
</div>
</div>
<div class="form-group row">
<label for="device-name" class="col-sm-2 col-form-label">Device:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="device-name" readonly value="@Model.DeviceID" />
</div>
</div>
<div class="form-group row">
<label for="agent-version" class="col-sm-2 col-form-label">Agent Version:</label>
<div class="col-sm-10">
Expand Down
2 changes: 2 additions & 0 deletions Server/Pages/EditDevice.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public EditDeviceModel(DataService dataService)
public string AgentVersion { get; set; }
public List<SelectListItem> DeviceGroups { get; } = new List<SelectListItem>();
public string DeviceName { get; set; }
public string DeviceID { get; set; }

[BindProperty]
public InputModel Input { get; set; } = new InputModel();
Expand Down Expand Up @@ -68,6 +69,7 @@ private void PopulateViewModel(string deviceID)
{
var device = DataService.GetDevice(user.OrganizationID, deviceID);
DeviceName = device?.DeviceName;
DeviceID = device?.ID;
AgentVersion = device.AgentVersion;
Input.Alias = device?.Alias;
Input.DeviceGroupID = device?.DeviceGroupID;
Expand Down
10 changes: 8 additions & 2 deletions Server/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public IndexModel(DataService dataService,

public string DefaultPrompt { get; set; }
public List<SelectListItem> DeviceGroups { get; set; } = new List<SelectListItem>();
public List<Alert> Alerts { get; set; } = new List<Alert>();
private ApplicationConfig AppConfig { get; }
private DataService DataService { get; }
private SignInManager<RemotelyUser> SignInManager { get; }
private ApplicationConfig AppConfig { get; }

public async Task<IActionResult> OnGet()
{
if (User?.Identity?.IsAuthenticated == true)
Expand All @@ -50,6 +50,12 @@ public async Task<IActionResult> OnGet()
{
DeviceGroups.AddRange(groups.Select(x => new SelectListItem(x.Name, x.ID)));
}
var alerts = DataService.GetAlerts(user.Id);
if (alerts.Any())
{
Alerts.AddRange(alerts);
}

}
else
{
Expand Down
31 changes: 29 additions & 2 deletions Server/Pages/_IndexLoggedIn.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
@model IndexModel


<button id="alertsButton" class="btn btn-info">
<span class="fa fa-bell"></span>
@Model.Alerts.Count
</button>

<div id="alertsFrame" class="bg-secondary">
<div class="text-right">
<span id="closeAlertsFrameButton" class="fa fa-times pointer mt-2 mr-2"></span>
</div>

<div class="mt-3">
@foreach (var alert in Model.Alerts)
{
<div class="card border-primary mb-3">
<div class="card-header">@alert.CreatedOn.ToString()</div>
<div class="card-body">
@if (!string.IsNullOrWhiteSpace(alert.Device?.DeviceName))
{
<h6 class="card-title">@alert.Device.DeviceName</h6>
}
<p class="card-text">@alert.Message</p>
</div>
</div>
}
</div>

</div>

<div class="work-area hidden">
<ul class="nav nav-tabs">
<li class="nav-item">
Expand All @@ -24,8 +53,6 @@
<partial name="_ConsoleFrame" model="Model" />
</div>
</div>


</div>

<script src="~/scripts/Main.js" type="module"></script>
Expand Down
51 changes: 34 additions & 17 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ public DataService(ApplicationDbContext context,

public async Task AddAlert(AlertOptions alertOptions, string organizationID)
{
await RemotelyContext.Users
var users = RemotelyContext.Users
.Include(x => x.Alerts)
.Where(x => x.OrganizationID == organizationID)
.ForEachAsync(x => {
var alert = new Alert()
{
CreatedOn = DateTimeOffset.Now,
DeviceID = alertOptions.AlertDeviceID,
Message = alertOptions.AlertMessage,
OrganizationID = organizationID
};
x.Alerts.Add(alert);
});
.Where(x => x.OrganizationID == organizationID);

if (!string.IsNullOrWhiteSpace(alertOptions.AlertDeviceID))
{
var filteredUserIDs = FilterUsersByDevicePermission(users.Select(x => x.Id), alertOptions.AlertDeviceID);
users = users.Where(x => filteredUserIDs.Contains(x.Id));
}

await users.ForEachAsync(x => {
var alert = new Alert()
{
CreatedOn = DateTimeOffset.Now,
DeviceID = alertOptions.AlertDeviceID,
Message = alertOptions.AlertMessage,
OrganizationID = organizationID
};
x.Alerts.Add(alert);
});

await RemotelyContext.SaveChangesAsync();
}

Expand Down Expand Up @@ -83,11 +90,6 @@ public bool AddDeviceGroup(string orgID, DeviceGroup deviceGroup, out string dev
return true;
}

public async Task<Alert> GetAlert(string alertID)
{
return await RemotelyContext.Alerts.FirstOrDefaultAsync(x => x.ID == alertID);
}

public InviteLink AddInvite(string orgID, Invite invite)
{
invite.InvitedUser = invite.InvitedUser.ToLower();
Expand Down Expand Up @@ -485,6 +487,21 @@ public string[] FilterUsersByDevicePermission(IEnumerable<string> userIDs, strin
.ToArray();
}

public async Task<Alert> GetAlert(string alertID)
{
return await RemotelyContext.Alerts
.Include(x => x.Device)
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.ID == alertID);
}

public IEnumerable<Alert> GetAlerts(string userID)
{
return RemotelyContext.Alerts
.Include(x => x.Device)
.Include(x => x.User)
.Where(x => x.UserID == userID);
}
public IEnumerable<ApiToken> GetAllApiTokens(string userID)
{
var user = RemotelyContext.Users.FirstOrDefault(x => x.Id == userID);
Expand Down
2 changes: 1 addition & 1 deletion Server/wwwroot/css/Themes/cyborg.custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ button.navbar-toggler:hover {
color: limegreen;
}

.fa-times {
td .fa-times {
color: red;
}

Expand Down
2 changes: 1 addition & 1 deletion Server/wwwroot/css/Themes/yeti.custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ button.navbar-toggler:hover {
color: forestgreen;
}

.fa-times {
td .fa-times {
color: red;
}

Expand Down
33 changes: 33 additions & 0 deletions Server/wwwroot/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,39 @@ span.label.code {
margin-bottom: -5px;
}


#alertsButton {
position: absolute;
top: 75px;
right: 5px;
z-index: 2;
transition: .5s ease left;
user-select: none;
}


#alertsFrame {
position: absolute;
right: 0;
top: 0;
height: 100%;
width: 0;
overflow-x: hidden;
overflow-y: auto;
text-align: center;
opacity: 0;
z-index: 2;
transition: .5s ease all;
}

#alertsFrame.open {
width: 350px;
opacity: 1;
transition: .5s ease all;
}



.float-message {
position: fixed;
top: 20px;
Expand Down
9 changes: 9 additions & 0 deletions Server/wwwroot/scripts/InputEventHandlers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 869fe9a

Please sign in to comment.