Skip to content

Commit

Permalink
Added login api
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Jan 25, 2022
1 parent 1f26368 commit 5610b64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
25 changes: 23 additions & 2 deletions Library.Encyclopedia.API/Controllers/EncylopediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using Library.Encyclopedia.Entity.Interfaces;
using Library.Encyclopedia.Entity.Models;
using Library.Encyclopedia.Entity.Models.External;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Threading.Tasks;

namespace Library.Encyclopedia.Controllers
Expand All @@ -35,6 +37,25 @@ public EncylopediaController(ILogger<EncylopediaController> logger, IApplication
this.mainDataAccess = new MainDataAccess(dbContext, configuration);
}

[HttpGet("Login")]
[Authorize(Roles = @"EncyclopediaAdministrators")]
public IActionResult Login()
{
try
{
using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine))
{
var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name);
return Ok(usr.DisplayName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

/// <summary>
/// Get all items based on search query
/// </summary>
Expand Down Expand Up @@ -86,7 +107,7 @@ public async Task<IActionResult> GetByCategory(string category,
{
try
{
var response = await mainDataAccess.GetByCategoryAsync(category, offset, limit,previewSize, asc);
var response = await mainDataAccess.GetByCategoryAsync(category, offset, limit, previewSize, asc);

if (response == null)
{
Expand Down Expand Up @@ -120,7 +141,7 @@ public async Task<IActionResult> GetByStartingAlphabet(char alphabet,
{
try
{
var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, limit,previewSize, asc);
var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, limit, previewSize, asc);

if (response == null)
{
Expand Down
4 changes: 4 additions & 0 deletions Library.Encyclopedia.API/Library.Encyclopedia.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Library.Encyclopedia.DataAccess\Library.Encyclopedia.DataAccess.csproj" />
<ProjectReference Include="..\Library.Encyclopedia.Entity\Library.Encyclopedia.Entity.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public async Task<Guid> CreateAsync(Main model)

// assign a random id
model.Id = Guid.NewGuid();
model.Category = model.Category == string.Empty ? null : model.Category;

model = ReplaceInternalLinks(model);
model = ReplaceLineBreaks(model);
Expand Down Expand Up @@ -329,7 +330,7 @@ public async Task<Main> UpdateAsync(Guid id, MainUpdateModel model)
entry.RawDescription = StripHTML(entry.RawDescription);
}

entry.Category = model.Category ?? entry.Category;
entry.Category = model.Category == null ? entry.Category : model.Category == string.Empty ? null : model.Category;

_dbcontext.Main.Update(entry);
await _dbcontext.SaveChanges();
Expand Down

0 comments on commit 5610b64

Please sign in to comment.