summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/AdminController.cs
blob: 91cbf810378289a729ddf699d1b7b3c74d580d58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    //We have a custom ACL implementation so we do not need to use the ASP.NET role system to check if a user has an ACL rule.
    [Authorize]
    [RequiresAdmin]
    public class AdminController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        public ActionResult Index(string id = "home")
        {
            ViewBag.Page = id;
            return View();
        }

        public ActionResult AddUserToRole(string id)
        {
            var model = new AddUserToRoleViewModel();
            model.Roles = new List<SelectListItem>();
            var db = new ApplicationDbContext();
            foreach(var r in db.Roles.ToArray())
            {
                var converted = r as Role;
                model.Roles.Add(new SelectListItem
                {
                    Text = converted.Name,
                    Value = converted.Name
                });
                if (converted.Id == id)
                    model.RoleId = converted.Name;
            }
            model.Users = new List<SelectListItem>();
            foreach(var u in db.Users.OrderBy(x => x.DisplayName).ToArray())
            {
                model.Users.Add(new SelectListItem
                {
                    Text = u.DisplayName,
                    Value = u.Id
                });
            }
            model.Username = db.Users.First().Id;
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddUserToRole(AddUserToRoleViewModel model)
        {
            var usermanager = HttpContext.GetOwinContext().Get<ApplicationUserManager>();
            usermanager.AddToRole(model.Username, model.RoleId);
            return RedirectToAction("Index", new { id = "roles" });
        }

        public ActionResult RemoveUserFromRole(string id, string usr)
        {
            var usermanager = HttpContext.GetOwinContext().Get<ApplicationUserManager>();
            var db = new ApplicationDbContext();
            Role role = null;
            foreach (var r in db.Roles)
            {
                if (r is Role)
                    if ((r as Role).Id == id)
                        role = r as Role;
            }
            usermanager.RemoveFromRole(usr, role.Name);
            return RedirectToAction("Index", new { id = "roles" });
        }

        public ActionResult RoleDetails(string id)
        {
            var db = new ApplicationDbContext();
            Role role = null;
            foreach (var r in db.Roles.ToArray())
            {
                if (r is Role)
                    if ((r as Role).Id == id)
                        role = r as Role;
            }
            return View(role);
        }
    }
}