Skip to content

Commit 20c89a4

Browse files
committed
First pass at application server level authorization support.
1 parent d8dd4f5 commit 20c89a4

File tree

7 files changed

+89
-47
lines changed

7 files changed

+89
-47
lines changed

src/Infrastructure.Server/NoteTaker.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ IMapper mapper
3737
// Work with notes with regard for categories.
3838
// In esseence these notes will be stored in the context of default category.
3939

40-
public NoteDto TakeNote(NewNoteMessage newNoteMessage)
40+
public NoteDto TakeNote(SecurityContext securityContext, NewNoteMessage newNoteMessage)
4141
{
4242
var category = GetDefaultCategory();
4343

@@ -47,7 +47,7 @@ public NoteDto TakeNote(NewNoteMessage newNoteMessage)
4747
return _mapper.Map<NoteDto>(note);
4848
}
4949

50-
public NoteDto ReadNote(Guid noteId)
50+
public NoteDto ReadNote(SecurityContext securityContext, Guid noteId)
5151
{
5252
var category = GetDefaultCategory();
5353

@@ -56,15 +56,15 @@ public NoteDto ReadNote(Guid noteId)
5656
return _mapper.Map<NoteDto>(note);
5757
}
5858

59-
public void RemoveNote(Guid noteId)
59+
public void RemoveNote(SecurityContext securityContext, Guid noteId)
6060
{
6161
var category = GetDefaultCategory();
6262

6363
category.RemoveNote(noteId);
6464
_categoryRepository.Save(category);
6565
}
6666

67-
public IList<NoteDto> ListNotes()
67+
public IList<NoteDto> ListNotes(SecurityContext securityContext)
6868
{
6969
var category = GetDefaultCategory();
7070

@@ -76,15 +76,15 @@ public IList<NoteDto> ListNotes()
7676
//
7777
// Work with categories
7878

79-
public IList<CategoryDto> ListCategories()
79+
public IList<CategoryDto> ListCategories(SecurityContext securityContext)
8080
{
8181
var categories = _categoryRepository.GetAll();
8282
if (null == categories) throw new NotFoundException("No Categories found.");
8383

8484
return _mapper.Map<IList<CategoryDto>>(categories);
8585
}
8686

87-
public CategoryDto CreateNewCategory(NewCategoryMessage newCategoryMessage)
87+
public CategoryDto CreateNewCategory(SecurityContext securityContext, NewCategoryMessage newCategoryMessage)
8888
{
8989
if (newCategoryMessage == null)
9090
{
@@ -102,7 +102,7 @@ public CategoryDto CreateNewCategory(NewCategoryMessage newCategoryMessage)
102102
return _mapper.Map<CategoryDto>(category);
103103
}
104104

105-
public CategoryDto RenameCategory(Guid categoryId, string newCategoryName)
105+
public CategoryDto RenameCategory(SecurityContext securityContext, Guid categoryId, string newCategoryName)
106106
{
107107
if (string.IsNullOrEmpty(newCategoryName))
108108
{
@@ -124,22 +124,22 @@ public CategoryDto RenameCategory(Guid categoryId, string newCategoryName)
124124
return _mapper.Map<CategoryDto>(category);
125125
}
126126

127-
public CategoryDto GetCategoryDetail(Guid categoryId)
127+
public CategoryDto GetCategoryDetail(SecurityContext securityContext, Guid categoryId)
128128
{
129129
var category = _categoryRepository.Get(categoryId);
130130
if (null == category) throw new NotFoundException("Category not found.");
131131

132132
return _mapper.Map<CategoryDto>(category);
133133
}
134134

135-
public void RemoveCategory(Guid categoryId)
135+
public void RemoveCategory(SecurityContext securityContext, Guid categoryId)
136136
{
137137
throw new NotImplementedException();
138138
}
139139

140140
// Work with categorized notes
141141

142-
public CategoryDto RemoveCategorizedNote(Guid categoryId, Guid noteId)
142+
public CategoryDto RemoveCategorizedNote(SecurityContext securityContext, Guid categoryId, Guid noteId)
143143
{
144144
var category = _categoryRepository.Get(categoryId);
145145
if (null == category) throw new NotFoundException("Category not found.");
@@ -156,7 +156,7 @@ public CategoryDto RemoveCategorizedNote(Guid categoryId, Guid noteId)
156156
return _mapper.Map<CategoryDto>(category);
157157
}
158158

159-
public NoteDto TakeCategorizedNote(Guid categoryId, NewNoteMessage newNoteMessage)
159+
public NoteDto TakeCategorizedNote(SecurityContext securityContext, Guid categoryId, NewNoteMessage newNoteMessage)
160160
{
161161
if (newNoteMessage == null)
162162
{
@@ -171,13 +171,13 @@ public NoteDto TakeCategorizedNote(Guid categoryId, NewNoteMessage newNoteMessag
171171
return _mapper.Map<NoteDto>(note);
172172
}
173173

174-
public IList<NoteDto> ListCategorizedNotes(Guid categoryId)
174+
public IList<NoteDto> ListCategorizedNotes(SecurityContext securityContext, Guid categoryId)
175175
{
176176
var category = GetCategory(categoryId);
177177
return _mapper.Map<IList<NoteDto>>(category.Notes);
178178
}
179179

180-
public NoteDto ReadCategorizedNote(Guid categoryId, Guid noteId)
180+
public NoteDto ReadCategorizedNote(SecurityContext securityContext, Guid categoryId, Guid noteId)
181181
{
182182
var category = GetCategory(categoryId);
183183
var note = category.RevealNote(noteId);

src/Infrastructure.Server/Registrar.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ namespace CompanyName.Notebook.NoteTaking.Infrastructure.Server
66

77
public class Registrar : IRegistrar
88
{
9-
public SubscriberDto Subscribe(string emailAddress)
9+
public SubscriberDto Subscribe(SecurityContext securityContext, string emailAddress)
1010
{
1111
throw new NotImplementedException();
1212
}
1313

14-
public SubscriberDto Subscribe(Guid categoryId, string emailAddress)
14+
public SubscriberDto Subscribe(SecurityContext securityContext, Guid categoryId, string emailAddress)
1515
{
1616
throw new NotImplementedException();
1717
}
1818

19-
public void Unsubscribe(Guid subscriberId)
19+
public void Unsubscribe(SecurityContext securityContext, Guid subscriberId)
2020
{
2121
throw new NotImplementedException();
2222
}
2323

24-
public void Unsubscribe(Guid categoryId, Guid subscriberId)
24+
public void Unsubscribe(SecurityContext securityContext, Guid categoryId, Guid subscriberId)
2525
{
2626
throw new NotImplementedException();
2727
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1.Bases
2+
{
3+
using System;
4+
using CompanyName.Notebook.NoteTaking.Core.Application.Messages;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
public abstract class NoteBookBaseController : Controller
8+
{
9+
protected SecurityContext SecurityContext
10+
{
11+
get {
12+
var securityContext = new SecurityContext
13+
{
14+
UserId = User.Identity.Name,
15+
Token = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1],
16+
ApiRoute = HttpContext.Request.Path + HttpContext.Request.QueryString,
17+
HttpAction = HttpContext.Request.Method
18+
};
19+
20+
return securityContext;
21+
}
22+
}
23+
}
24+
}

src/Infrastructure.WebApi/Controllers/v1/CategoriesController.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1
44
using System.Collections.Generic;
55
using CompanyName.Notebook.NoteTaking.Core.Application.Messages;
66
using CompanyName.Notebook.NoteTaking.Core.Application.Services;
7+
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1.Bases;
78
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Validators;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.Extensions.Logging;
1011

1112
[Route("api/v1/[controller]")]
12-
public class CategoriesController : Controller
13+
public class CategoriesController : NoteBookBaseController
1314
{
1415
private ILogger _logger;
1516
private readonly INoteTaker _noteTaker;
@@ -32,7 +33,7 @@ public CategoriesController(
3233
[ProducesResponseType(typeof(BadRequestResult), 400)]
3334
public IActionResult Get()
3435
{
35-
var categoryDtos = _noteTaker.ListCategories();
36+
var categoryDtos = _noteTaker.ListCategories(SecurityContext);
3637
return Ok(categoryDtos);
3738
}
3839

@@ -48,7 +49,7 @@ public IActionResult Get()
4849
[ProducesResponseType(typeof(BadRequestResult), 400)]
4950
public IActionResult Get(Guid id)
5051
{
51-
var categoryDto = _noteTaker.GetCategoryDetail(id);
52+
var categoryDto = _noteTaker.GetCategoryDetail(SecurityContext, id);
5253
return Ok(categoryDto);
5354
}
5455

@@ -65,7 +66,7 @@ public IActionResult Get(Guid id)
6566
[ProducesResponseType(typeof(BadRequestResult), 400)]
6667
public IActionResult Post([FromBody]NewCategoryMessage newCategoryMessage)
6768
{
68-
var categoryDto = _noteTaker.CreateNewCategory(newCategoryMessage);
69+
var categoryDto = _noteTaker.CreateNewCategory(SecurityContext, newCategoryMessage);
6970
return CreatedAtAction("Get", new { id = categoryDto.Id }, categoryDto);
7071
}
7172

@@ -81,7 +82,7 @@ public IActionResult Post([FromBody]NewCategoryMessage newCategoryMessage)
8182
[ProducesResponseType(typeof(BadRequestResult), 400)]
8283
public IActionResult Put(Guid id, [FromBody]CategoryDto updatedCategory)
8384
{
84-
var categoryDto = _noteTaker.RenameCategory(id, updatedCategory.Name);
85+
var categoryDto = _noteTaker.RenameCategory(SecurityContext, id, updatedCategory.Name);
8586
return CreatedAtAction("Get", new { id = categoryDto.Id }, categoryDto);
8687
}
8788

@@ -96,7 +97,7 @@ public IActionResult Put(Guid id, [FromBody]CategoryDto updatedCategory)
9697
[ProducesResponseType(typeof(BadRequestResult), 400)]
9798
public IActionResult Delete(Guid id)
9899
{
99-
_noteTaker.RemoveCategory(id);
100+
_noteTaker.RemoveCategory(SecurityContext, id);
100101
return NoContent();
101102
}
102103

@@ -112,7 +113,7 @@ public IActionResult Delete(Guid id)
112113
[ProducesResponseType(typeof(BadRequestResult), 400)]
113114
public IActionResult GetNotesFromCategory(Guid id)
114115
{
115-
var noteDtos =_noteTaker.ListCategorizedNotes(id);
116+
var noteDtos =_noteTaker.ListCategorizedNotes(SecurityContext, id);
116117
return Ok(noteDtos);
117118
}
118119

@@ -129,7 +130,7 @@ public IActionResult GetNotesFromCategory(Guid id)
129130
[ProducesResponseType(typeof(BadRequestResult), 400)]
130131
public IActionResult CreateNoteInCategory(Guid id, [FromBody]NewNoteMessage newNoteMessage)
131132
{
132-
var noteDto =_noteTaker.TakeCategorizedNote(id, newNoteMessage);
133+
var noteDto =_noteTaker.TakeCategorizedNote(SecurityContext, id, newNoteMessage);
133134
return CreatedAtRoute("ReadCategorizedNote", new { noteId = noteDto.Id }, noteDto);
134135
}
135136

@@ -146,7 +147,7 @@ public IActionResult CreateNoteInCategory(Guid id, [FromBody]NewNoteMessage newN
146147
[ProducesResponseType(typeof(BadRequestResult), 400)]
147148
public IActionResult GetNoteFromCategory(Guid id, Guid noteId)
148149
{
149-
var noteDto =_noteTaker.ReadCategorizedNote(id, noteId);
150+
var noteDto =_noteTaker.ReadCategorizedNote(SecurityContext, id, noteId);
150151
return Ok(noteDto);
151152
}
152153

@@ -163,7 +164,7 @@ public IActionResult GetNoteFromCategory(Guid id, Guid noteId)
163164
[ProducesResponseType(typeof(BadRequestResult), 400)]
164165
public IActionResult DeleteCategorizedNote(Guid id, Guid noteId)
165166
{
166-
_noteTaker.RemoveCategorizedNote(id, noteId);
167+
_noteTaker.RemoveCategorizedNote(SecurityContext, id, noteId);
167168
return NoContent();
168169
}
169170

src/Infrastructure.WebApi/Controllers/v1/NotesController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1
44
using System.Collections.Generic;
55
using CompanyName.Notebook.NoteTaking.Core.Application.Messages;
66
using CompanyName.Notebook.NoteTaking.Core.Application.Services;
7+
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Controllers.v1.Bases;
78
using CompanyName.Notebook.NoteTaking.Infrastructure.WebApi.Validators;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.Extensions.Logging;
1011

1112
[Route("api/v1/[controller]")]
12-
public class NotesController : Controller
13+
public class NotesController : NoteBookBaseController
1314
{
1415
private ILogger _logger;
1516
private readonly INoteTaker _noteTaker;
@@ -32,7 +33,7 @@ public NotesController(
3233
[ProducesResponseType(typeof(BadRequestResult), 400)]
3334
public IActionResult Get()
3435
{
35-
var noteDtos =_noteTaker.ListNotes();
36+
var noteDtos =_noteTaker.ListNotes(SecurityContext);
3637
return Ok(noteDtos);
3738
}
3839

@@ -48,7 +49,7 @@ public IActionResult Get()
4849
[ProducesResponseType(typeof(BadRequestResult), 400)]
4950
public IActionResult GetNote(Guid id)
5051
{
51-
var noteDto = _noteTaker.ReadNote(id);
52+
var noteDto = _noteTaker.ReadNote(SecurityContext, id);
5253
return Ok(noteDto);
5354
}
5455

@@ -65,7 +66,7 @@ public IActionResult GetNote(Guid id)
6566
[ProducesResponseType(typeof(BadRequestResult), 400)]
6667
public IActionResult Post([FromBody]NewNoteMessage newNoteMessage)
6768
{
68-
var noteDto = _noteTaker.TakeNote(newNoteMessage);
69+
var noteDto = _noteTaker.TakeNote(SecurityContext, newNoteMessage);
6970
return CreatedAtRoute("GetNote", new { id = noteDto.Id }, noteDto);
7071
}
7172

@@ -80,7 +81,7 @@ public IActionResult Post([FromBody]NewNoteMessage newNoteMessage)
8081
[ProducesResponseType(typeof(BadRequestResult), 400)]
8182
public IActionResult Delete(Guid id)
8283
{
83-
_noteTaker.RemoveNote(id);
84+
_noteTaker.RemoveNote(SecurityContext, id);
8485
return NoContent();
8586
}
8687
}

0 commit comments

Comments
 (0)