Un utilisitateur a plusieurs profiles
Il en a un préféré. Il peux en saisir de tout type.
This commit is contained in:
158
Yavsc/Controllers/DoController.cs
Normal file
158
Yavsc/Controllers/DoController.cs
Normal file
@ -0,0 +1,158 @@
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNet.Authorization;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Data.Entity;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Workflow;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
public class DoController : Controller
|
||||
{
|
||||
private ApplicationDbContext _context;
|
||||
|
||||
public DoController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: Do
|
||||
[HttpGet,ActionName("Index")]
|
||||
public IActionResult Index(string id)
|
||||
{
|
||||
if (id == null)
|
||||
id = User.GetUserId();
|
||||
|
||||
var applicationDbContext = _context.UserActivities.Include(u => u.Does).Include(u => u.User).Where(u=> u.UserId == id);
|
||||
return View(applicationDbContext.ToList());
|
||||
}
|
||||
|
||||
// GET: Do/Details/5
|
||||
public IActionResult Details(long? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
UserActivity userActivity = _context.UserActivities.Single(m => m.Id == id);
|
||||
if (userActivity == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
return View(userActivity);
|
||||
}
|
||||
|
||||
// GET: Do/Create
|
||||
[ActionName("Create"),Authorize]
|
||||
public IActionResult Create(string userId)
|
||||
{
|
||||
if (userId==null)
|
||||
userId = User.GetUserId();
|
||||
ViewBag.DoesCode = new SelectList(_context.Activities, "Code", "Name");
|
||||
//ViewData["UserId"] = userId;
|
||||
ViewBag.UserId = new SelectList(_context.Performers.Include(p=>p.Performer), "PerformerId", "Performer", userId);
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Do/Create
|
||||
[HttpPost(),ActionName("Create"),Authorize]
|
||||
[ValidateAntiForgeryToken]
|
||||
public IActionResult Create(UserActivity userActivity)
|
||||
{
|
||||
var uid = User.GetUserId();
|
||||
if (!User.IsInRole("Administrator"))
|
||||
if (uid != userActivity.UserId)
|
||||
ModelState.AddModelError("User","You're not admin.");
|
||||
if (userActivity.UserId == null) userActivity.UserId = uid;
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.UserActivities.Add(userActivity);
|
||||
_context.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
ViewBag.DoesCode = new SelectList(_context.Activities, "Code", "Name", userActivity.DoesCode);
|
||||
ViewBag.UserId = new SelectList(_context.Performers.Include(p=>p.Performer), "PerformerId", "User", userActivity.UserId);
|
||||
return View(userActivity);
|
||||
}
|
||||
|
||||
// GET: Do/Edit/5
|
||||
[Authorize]
|
||||
public IActionResult Edit(long? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
UserActivity userActivity = _context.UserActivities.Single(m => m.Id == id);
|
||||
if (userActivity == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
ViewData["DoesCode"] = new SelectList(_context.Activities, "Code", "Does", userActivity.DoesCode);
|
||||
ViewData["UserId"] = new SelectList(_context.Performers, "PerformerId", "User", userActivity.UserId);
|
||||
return View(userActivity);
|
||||
}
|
||||
|
||||
// POST: Do/Edit/5
|
||||
[HttpPost,Authorize]
|
||||
[ValidateAntiForgeryToken]
|
||||
public IActionResult Edit(UserActivity userActivity)
|
||||
{
|
||||
if (!User.IsInRole("Administrator"))
|
||||
if (User.GetUserId() != userActivity.UserId)
|
||||
ModelState.AddModelError("User","You're not admin.");
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_context.Update(userActivity);
|
||||
_context.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
ViewData["DoesCode"] = new SelectList(_context.Activities, "Code", "Does", userActivity.DoesCode);
|
||||
ViewData["UserId"] = new SelectList(_context.Performers, "PerformerId", "User", userActivity.UserId);
|
||||
return View(userActivity);
|
||||
}
|
||||
|
||||
// GET: Do/Delete/5
|
||||
[ActionName("Delete"),Authorize]
|
||||
public IActionResult Delete(long? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
UserActivity userActivity = _context.UserActivities.Single(m => m.Id == id);
|
||||
|
||||
if (userActivity == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
if (!User.IsInRole("Administrator"))
|
||||
if (User.GetUserId() != userActivity.UserId)
|
||||
ModelState.AddModelError("User","You're not admin.");
|
||||
return View(userActivity);
|
||||
}
|
||||
|
||||
// POST: Do/Delete/5
|
||||
[HttpPost, ActionName("Delete"),Authorize]
|
||||
[ValidateAntiForgeryToken]
|
||||
public IActionResult DeleteConfirmed(long id)
|
||||
{
|
||||
UserActivity userActivity = _context.UserActivities.Single(m => m.Id == id);
|
||||
if (!User.IsInRole("Administrator"))
|
||||
if (User.GetUserId() != userActivity.UserId) {
|
||||
ModelState.AddModelError("User","You're not admin.");
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
_context.UserActivities.Remove(userActivity);
|
||||
_context.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
}
|
@ -43,14 +43,13 @@ namespace Yavsc.Controllers
|
||||
throw new NotImplementedException("No Activity code");
|
||||
}
|
||||
|
||||
ViewBag.Activities = _context.ActivityItems(id);
|
||||
ViewBag.Activity = _context.Activities.FirstOrDefault(
|
||||
a => a.Code == id);
|
||||
|
||||
return View(
|
||||
_context.Performers.Include(p => p.Performer)
|
||||
.Include(p=>p.Performer.Devices).Where
|
||||
(p => p.ActivityCode == id && p.Active).OrderBy(
|
||||
(p => p.Activity.Any( a => a.DoesCode == id) && p.Active).OrderBy(
|
||||
x => x.MinDailyCost
|
||||
)
|
||||
);
|
||||
|
@ -112,8 +112,8 @@ namespace Yavsc.Controllers
|
||||
};
|
||||
if (_dbContext.Performers.Any(x => x.PerformerId == user.Id))
|
||||
{
|
||||
var code = _dbContext.Performers.First(x => x.PerformerId == user.Id).ActivityCode;
|
||||
model.Activity = _dbContext.Activities.First(x => x.Code == code);
|
||||
model.Activity = _dbContext.Performers.First(x => x.PerformerId == user.Id).Activity;
|
||||
|
||||
}
|
||||
return View(model);
|
||||
}
|
||||
@ -491,16 +491,19 @@ namespace Yavsc.Controllers
|
||||
{
|
||||
var user = GetCurrentUserAsync().Result;
|
||||
var uid = user.Id;
|
||||
bool existing = _dbContext.Performers.Any(x => x.PerformerId == uid);
|
||||
ViewBag.Activities = _dbContext.ActivityItems(null);
|
||||
var existing = _dbContext.Performers.Include(x => x.OrganizationAddress)
|
||||
.Include(p=>p.Activity).FirstOrDefault(x => x.PerformerId == uid);
|
||||
|
||||
ViewBag.GoogleSettings = _googleSettings;
|
||||
if (existing)
|
||||
if (existing!=null)
|
||||
{
|
||||
var currentProfile = _dbContext.Performers.Include(x => x.OrganizationAddress)
|
||||
.First(x => x.PerformerId == uid);
|
||||
string currentCode = currentProfile.ActivityCode;
|
||||
ViewBag.Activities = _dbContext.ActivityItems(existing.Activity);
|
||||
return View(currentProfile);
|
||||
}
|
||||
|
||||
ViewBag.Activities = _dbContext.ActivityItems(new List<UserActivity>());
|
||||
return View(new PerformerProfile
|
||||
{
|
||||
PerformerId = user.Id,
|
||||
@ -535,7 +538,7 @@ namespace Yavsc.Controllers
|
||||
"SIREN",
|
||||
_SR["Invalid company number"] + " (" + taskCheck.errorCode + ")"
|
||||
);
|
||||
_logger.LogInformation("Invalid company number, using key:" + _cinfoSettings.ApiKey);
|
||||
_logger.LogInformation($"Invalid company number: {model.SIREN}/{taskCheck.errorType}/{taskCheck.errorCode}/{taskCheck.errorMessage}" );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -543,6 +546,7 @@ namespace Yavsc.Controllers
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
ModelState.AddModelError("SIREN", ex.Message);
|
||||
}
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
@ -553,13 +557,12 @@ namespace Yavsc.Controllers
|
||||
{
|
||||
_dbContext.Map.Add(model.OrganizationAddress);
|
||||
}
|
||||
bool existing = _dbContext.Performers.Any(x => x.PerformerId == uid);
|
||||
if (existing)
|
||||
|
||||
if (_dbContext.Performers.Any(p=>p.PerformerId == uid))
|
||||
{
|
||||
_dbContext.Update(model);
|
||||
}
|
||||
else _dbContext.Performers.Add(model);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
// Give this user the Performer role
|
||||
if (!User.IsInRole("Performer"))
|
||||
@ -572,7 +575,7 @@ namespace Yavsc.Controllers
|
||||
else ModelState.AddModelError(string.Empty, $"Access denied ({uid} vs {model.PerformerId})");
|
||||
}
|
||||
ViewBag.GoogleSettings = _googleSettings;
|
||||
ViewBag.Activities = _dbContext.ActivityItems(model.ActivityCode);
|
||||
ViewBag.Activities = _dbContext.ActivityItems(model.Activity);
|
||||
return View(model);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user