Skip to content

Commit e82ac26

Browse files
authored
Merge pull request #56 from Purdue-ACM-SIGAPP/DotNet-Hide-URL
Dot net hide url
2 parents dd2677b + 439d27f commit e82ac26

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

Controllers/EventsController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Authorization;
12
using Microsoft.AspNetCore.Mvc;
23
using SimpleWebAppReact.Entities;
34
using Microsoft.Extensions.Logging;
@@ -96,6 +97,8 @@ public async Task<IEnumerable<Events>> Get([FromQuery] string? eventName = null,
9697
/// <param name="events"></param>
9798
/// <returns></returns>
9899
[HttpPost]
100+
//roles that can edit events
101+
[Authorize(Roles ="RA, Club, Greek Life Officer")]
99102
public async Task<ActionResult> Post(Events events)
100103
{
101104
await _events.InsertOneAsync(events);
@@ -109,6 +112,8 @@ public async Task<ActionResult> Post(Events events)
109112
/// <param name="events"></param>
110113
/// <returns></returns>
111114
[HttpPut]
115+
//roles that can edit events
116+
[Authorize(Roles ="RA, Club, Greek Life Officer")]
112117
public async Task<ActionResult> Update(Events events)
113118
{
114119
var filter = Builders<Events>.Filter.Eq(x => x.Id, events.Id);
@@ -122,6 +127,8 @@ public async Task<ActionResult> Update(Events events)
122127
/// <param name="id"></param>
123128
/// <returns></returns>
124129
[HttpDelete("{id}")]
130+
//roles that can edit events
131+
[Authorize(Roles ="RA, Club, Greek Life Officer")]
125132
public async Task<ActionResult> Delete(string id)
126133
{
127134
var filter = Builders<Events>.Filter.Eq(x => x.Id, id);

Controllers/UserController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Mvc;
22
using SimpleWebAppReact.Entities;
33
using Microsoft.Extensions.Logging;
4+
using MongoDB.Bson;
45
using MongoDB.Driver;
56
using SimpleWebAppReact.Services;
67

@@ -72,13 +73,14 @@ public async Task<IEnumerable<User>> Get([FromQuery] string? name = null, [FromQ
7273
[HttpGet("{id}")]
7374
public async Task<ActionResult<User?>> GetById(string id)
7475
{
76+
ObjectId objectId = new ObjectId(id);
7577
// Simple validation to check if the ID is not null
7678
if (string.IsNullOrEmpty(id))
7779
{
7880
return BadRequest("Invalid ID format.");
7981
}
8082

81-
var filter = Builders<User>.Filter.Eq(x => x.Id, id);
83+
var filter = Builders<User>.Filter.Eq(x => x.Id, objectId);
8284
var user = _users.Find(filter).FirstOrDefault();
8385
return user is not null ? Ok(user) : NotFound();
8486
}
@@ -117,7 +119,8 @@ public async Task<ActionResult> Update(User user)
117119
[HttpDelete("{id}")]
118120
public async Task<ActionResult> Delete(string id)
119121
{
120-
var filter = Builders<User>.Filter.Eq(x => x.Id, id);
122+
ObjectId objectId = new ObjectId(id);
123+
var filter = Builders<User>.Filter.Eq(x => x.Id, objectId);
121124
await _users.DeleteOneAsync(filter);
122125
return Ok();
123126
}

Entities/User.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
using AspNetCore.Identity.Mongo.Model;
2+
13
namespace SimpleWebAppReact.Entities;
24
using MongoDB.Bson;
35
using MongoDB.Bson.Serialization.Attributes;
46
/// <summary>
57
/// Class structure matches 1-1 with User Table in database
68
/// </summary>
7-
public class User
9+
public class User : MongoUser
810
{
9-
[BsonId]
10-
[BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)]
11-
public string? Id { get; set; }
11+
// [BsonId]
12+
// [BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)]
13+
// public string? Id { get; set; }
1214

1315
[BsonElement("name"), BsonRepresentation(BsonType.String)]
1416
public string? Name { get; set; }

Program.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Security.Claims;
2+
using AspNetCore.Identity.Mongo;
3+
using AspNetCore.Identity.Mongo.Model;
24
using Microsoft.AspNetCore.Authentication.JwtBearer;
35
using Microsoft.AspNetCore.Authorization;
46
using Microsoft.AspNetCore.Identity;
57
using Microsoft.IdentityModel.Tokens;
68
using Microsoft.OpenApi.Models;
79
using SimpleWebAppReact;
10+
using SimpleWebAppReact.Entities;
811
using SimpleWebAppReact.Services;
912

1013
var builder = WebApplication.CreateBuilder(args);
@@ -47,12 +50,28 @@
4750
});
4851
});
4952
builder.Services.AddSingleton<MongoDbService>();
53+
// Here, configure User
54+
var connectionString = builder.Configuration.GetConnectionString("DbConnection");
55+
var databaseName = builder.Configuration.GetConnectionString("DatabaseName");
56+
57+
// At the ConfigureServices section in Startup.cs
58+
builder.Services.AddIdentityMongoDbProvider<User, MongoRole>(identity =>
59+
{
60+
identity.Password.RequiredLength = 8;
61+
// other options
62+
},
63+
mongo =>
64+
{
65+
mongo.ConnectionString = connectionString;
66+
// other options
67+
});
68+
5069
builder.Services.AddHttpClient<BuildingOutlineService>();
5170
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
5271
.AddJwtBearer(options =>
5372
{
54-
options.Authority = "https://dev-2gowyyl3kin685ua.us.auth0.com/";
55-
options.Audience = "http://localhost:5128";
73+
options.Authority = builder.Configuration.GetConnectionString("opt_Authority");
74+
options.Audience = builder.Configuration.GetConnectionString("opt_audience");
5675
options.TokenValidationParameters = new TokenValidationParameters
5776
{
5877
NameClaimType = ClaimTypes.NameIdentifier,

SimpleWebAppReact.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="AspNetCore.Identity.Mongo" Version="9.0.1-rc1" />
1112
<PackageReference Include="Auth0.AspNetCore.Authentication" Version="1.4.1" />
1213
<PackageReference Include="dotenv.net" Version="3.2.1" />
1314
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
1415
<PackageReference Include="FuzzySharp" Version="2.0.2" />
1516
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
1617
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.33" />
18+
<PackageReference Include="MongoDB.AspNet.Identity" Version="1.0.5" />
1719
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
1820
<PackageReference Include="Moq" Version="4.20.70" />
1921
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

0 commit comments

Comments
 (0)