-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from OpenIPC/nvr-hostname
add deviceconfigvalidator
- Loading branch information
Showing
3 changed files
with
127 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Newtonsoft.Json; | ||
using OpenIPC_Config.Models; | ||
|
||
namespace OpenIPC_Config.Services; | ||
|
||
public class DeviceConfigValidator | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly Dictionary<DeviceType, List<string>> _deviceHostnameMapping; | ||
|
||
public DeviceConfigValidator(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
|
||
_deviceHostnameMapping = _configuration.GetSection("DeviceHostnameMapping") | ||
.Get<Dictionary<DeviceType, List<string>>>() | ||
?? new Dictionary<DeviceType, List<string>>(); | ||
} | ||
|
||
public bool IsDeviceConfigValid(DeviceConfig deviceConfig) | ||
{ | ||
if (_deviceHostnameMapping.TryGetValue(deviceConfig.DeviceType, out var allowedHostnames)) | ||
{ | ||
return allowedHostnames.Any(hostname => deviceConfig.Hostname.Contains(hostname)); | ||
} | ||
|
||
return false; // Invalid if no mapping exists | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters