This commit is contained in:
2017-10-10 20:50:05 +02:00
parent 3487aa2a96
commit 59ff0cbfdd
39 changed files with 28108 additions and 155 deletions

View File

@ -30,6 +30,29 @@ namespace Yavsc.Controllers
this.context = context;
}
private async Task<bool> CreateRoles () {
// ensure all roles existence
foreach (string roleName in new string[] {
Constants.AdminGroupName,
Constants.StarGroupName,
Constants.PerformerGroupName,
Constants.FrontOfficeGroupName,
Constants.StarHunterGroupName,
Constants.BlogModeratorGroupName
})
if (!await _roleManager.RoleExistsAsync(roleName))
{
var role = new IdentityRole { Name = roleName };
var resultCreate = await _roleManager.CreateAsync(role);
if (!resultCreate.Succeeded)
{
AddErrors(resultCreate);
return false;
}
}
return true;
}
/// <summary>
/// Gives the (new if was not existing) administrator role
/// to current authenticated user, when no existing
@ -42,25 +65,18 @@ namespace Yavsc.Controllers
{
// If some amdin already exists, make this method disapear
var admins = await _userManager.GetUsersInRoleAsync(Constants.AdminGroupName);
if (admins != null && admins.Count > 0) return HttpNotFound();
// ensure all roles existence
foreach (string roleName in new string[] {Constants.AdminGroupName,
Constants.StarGroupName, Constants.PerformerGroupName,
Constants.FrontOfficeGroupName,
Constants.StarHunterGroupName
})
if (!await _roleManager.RoleExistsAsync(roleName))
if (admins != null && admins.Count > 0)
{
if (User.IsInRole(Constants.AdminGroupName))
{
var role = new IdentityRole { Name = roleName };
var resultCreate = await _roleManager.CreateAsync(role);
if (!resultCreate.Succeeded)
{
AddErrors(resultCreate);
// check all user groups exist
if (!await CreateRoles())
return new BadRequestObjectResult(ModelState);
}
return Ok(new { message = "you checked the role list." });
}
return HttpNotFound();
}
var user = await _userManager.FindByIdAsync(User.GetUserId());
IdentityRole adminRole;

View File

@ -0,0 +1,167 @@
using System.Threading.Tasks;
using Yavsc.ViewModels.Auth;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Messaging;
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.Rendering;
namespace Yavsc.Controllers
{
public class AnnouncesController : Controller
{
private ApplicationDbContext _context;
IStringLocalizer<AnnouncesController> _localizer;
IAuthorizationService _authorizationService;
public AnnouncesController(ApplicationDbContext context,
IAuthorizationService authorizationService,
IStringLocalizer<AnnouncesController> localizer)
{
_context = context;
_authorizationService = authorizationService;
_localizer = localizer;
}
// GET: Announces
public async Task<IActionResult> Index()
{
return View(await _context.Announce.ToListAsync());
}
// GET: Announces/Details/5
public async Task<IActionResult> Details(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Announce announce = await _context.Announce.SingleAsync(m => m.Id == id);
if (announce == null)
{
return HttpNotFound();
}
return View(announce);
}
// GET: Announces/Create
public async Task<IActionResult> Create()
{
var model = new Announce();
await SetupView(model);
return View(model);
}
private async Task SetupView(Announce announce)
{
ViewBag.IsAdmin = User.IsInRole(Constants.AdminGroupName);
ViewBag.IsPerformer = User.IsInRole(Constants.PerformerGroupName);
ViewBag.AllowEdit = (announce!=null && announce.Id>0) ?
await _authorizationService.AuthorizeAsync(User,announce,new EditRequirement()) :
true;
List<SelectListItem> dl = new List<SelectListItem>();
var rnames = System.Enum.GetNames(typeof(Reason));
var rvalues = System.Enum.GetValues(typeof(Reason));
for (int i = 0; i<rnames.Length; i++) {
dl.Add(new SelectListItem { Text =
_localizer[rnames[i]],
Value= rvalues.GetValue(i).ToString() });
}
ViewBag.For = dl.ToArray();
}
// POST: Announces/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Announce announce)
{
await SetupView(announce);
if (ModelState.IsValid)
{
// Only allow admin to create corporate annonces
if (announce.For == Reason.Corporate && ! ViewBag.IsAdmin)
{
ModelState.AddModelError("For", _localizer["YourNotAdmin"]);
return View(announce);
}
// Only allow performers to create ServiceProposal
if (announce.For == Reason.ServiceProposal && ! ViewBag.IsAdmin)
{
ModelState.AddModelError("For", _localizer["YourNotAPerformer"]);
return View(announce);
}
_context.Announce.Add(announce);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(announce);
}
// GET: Announces/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Announce announce = await _context.Announce.SingleAsync(m => m.Id == id);
if (announce == null)
{
return HttpNotFound();
}
return View(announce);
}
// POST: Announces/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Announce announce)
{
if (ModelState.IsValid)
{
_context.Update(announce);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(announce);
}
// GET: Announces/Delete/5
[ActionName("Delete")]
public async Task<IActionResult> Delete(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Announce announce = await _context.Announce.SingleAsync(m => m.Id == id);
if (announce == null)
{
return HttpNotFound();
}
return View(announce);
}
// POST: Announces/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id)
{
Announce announce = await _context.Announce.SingleAsync(m => m.Id == id);
_context.Announce.Remove(announce);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}

View File

@ -121,6 +121,7 @@ namespace Yavsc.Controllers
return new ChallengeResult();
}
ViewData["apicmtctlr"] = "/api/blogcomments";
ViewData["moderatoFlag"] = User.IsInRole(Constants.BlogModeratorGroupName);
return View(blog);
}

View File

@ -181,7 +181,7 @@ Le client final: {clientFinal}
return await Index();
}
/// <summary>
/// List client's queries
/// List client's queries (and only client's ones)
/// </summary>
/// <returns></returns>
public override async Task<IActionResult> Index()
@ -192,7 +192,7 @@ Le client final: {clientFinal}
.Include(x => x.PerformerProfile)
.Include(x => x.PerformerProfile.Performer)
.Include(x => x.Location)
.Where(x => x.ClientId == uid || x.PerformerId == uid)
.Where(x => x.ClientId == uid)
.ToListAsync());
}

View File

@ -53,15 +53,14 @@ namespace Yavsc.Controllers
n=> !clicked.Any(c=>n.Id==c)
);
this.Notify(notes);
ViewData["HasHaircutCommand"] = DbContext.HairCutQueries.Any
(q=>q.ClientId == uid && q.Status < QueryStatus.Failed);
ViewData["HaircutCommandCount"] = DbContext.HairCutQueries.Where(
q=>q.ClientId == uid && q.Status < QueryStatus.Failed
).Count();
if (id==null) {
// Workaround
// NotImplementedException: Remotion.Linq.Clauses.ResultOperators.ConcatResultOperator
//
// Use Concat()| whatever to do left outer join on ToArray() or ToList(), not on IQueryable
var legacy = DbContext.Activities
.Include(a=>a.Forms).Include(a=>a.Children)
.Where(a=> !a.Hidden)