Esempio n. 1
0
        public async Task <IActionResult> AddSupportUnit(SupportUnit unit, List <int> employeeIds, List <int> systemIds)
        {
            unit = _organizationService.AddSupportUnit(unit);

            var employees = _userService.GetEmployees(employeeIds);

            foreach (var employee in employees)
            {
                employee.SupportUnitId = unit.SupportUnitId;
            }
            _userService.SaveChanges();

            var systems = _systemService.GetSystems(systemIds);

            foreach (var system in systems)
            {
                system.SupportUnitId = unit.SupportUnitId;
            }
            _systemService.SaveChanges();

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.SupportUnit, unit.SupportUnitId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created support unit with id {unit.SupportUnitId}");

            return(RedirectToAction(nameof(ViewSupportUnit), new { id = unit.SupportUnitId }));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddLocation(Location location)
        {
            location = _organizationService.AddLocation(location);

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.Location, location.LocationId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created location with id {location.LocationId}");

            return(RedirectToAction(nameof(ViewLocation), new { id = location.LocationId }));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddBureau(Bureau bureau)
        {
            bureau = _organizationService.AddBureau(bureau);

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.Bureau, bureau.BureauId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created bureau with id {bureau.BureauId}");

            return(RedirectToAction(nameof(ViewBureau), new { id = bureau.BureauId }));
        }
Esempio n. 4
0
        public async Task <IActionResult> SubmitRequest(int id)
        {
            var request    = _requestService.GetRequest(id);
            var authResult = await _authService.AuthorizeAsync(User, request, "CanEditRequest");

            if (!authResult.Succeeded)
            {
                return(new ForbidResult());
            }

            request.RequestStatus = RequestStatus.UnderReview;
            request.SubmittedOn   = DateTime.Now;
            _requestService.SaveChanges();

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Submit, LogResourceType.Request, id,
                                   $"{identity.GetClaim(ClaimTypes.Name)} submitted request with id {id}");

            if (request.Reviews.Count > 0)
            {
                Employee reviewer   = request.OrderedReviews[0].Reviewer;
                string   receipient = reviewer.Email;
                string   emailName  = "ReviewRequest";
                var      model      = new { _emailHelper.AppUrl, _emailHelper.AppEmail, Request = request };
                string   subject    = _emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer);
                await _email.To(receipient)
                .Subject(subject)
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model)
                .SendAsync();
            }
            else
            {
                request.RequestStatus = RequestStatus.Approved;
                request.CompletedOn   = DateTime.Now;
                _requestService.SaveChanges();

                foreach (var requestedSystem in request.Systems)
                {
                    var systemAccess = new SystemAccess(request, requestedSystem);
                    _systemService.AddSystemAccess(systemAccess);
                }

                string emailName = "ProcessRequest";
                var    model     = new { _emailHelper.AppUrl, _emailHelper.AppEmail, Request = request };
                _email.Subject(_emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer))
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model);
                _email.Data.ToAddresses.Clear();
                var supportUnitIds = request.Systems.GroupBy(s => s.System.SupportUnitId, s => s).Select(g => g.Key).ToList();
                foreach (var supportUnitId in supportUnitIds)
                {
                    var supportUnit = _organizationService.GetSupportUnit((int)supportUnitId);
                    _email.To(supportUnit.Email);
                }
                await _email.SendAsync();
            }

            return(RedirectToAction("MyRequests"));
        }
Esempio n. 5
0
        public async Task <IActionResult> AddSystem(Models.System system, List <int> formIds)
        {
            system = _systemService.AddSystem(system);
            if (formIds.Count > 0)
            {
                foreach (var formId in formIds)
                {
                    system.Forms.Add(new SystemForm(system.SystemId, formId));
                }
                _systemService.SaveChanges();
            }

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.System, system.SystemId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created system with id {system.SystemId}");

            return(RedirectToAction(nameof(ViewSystem), new { id = system.SystemId }));
        }
Esempio n. 6
0
        public async Task <IActionResult> AddUnit(Unit unit, ICollection <int> systemIds)
        {
            unit.Systems = new List <UnitSystem>();
            foreach (var systemId in systemIds.OrderBy(v => v).ToList())
            {
                unit.Systems.Add(new UnitSystem()
                {
                    SystemId = systemId
                });
            }
            unit = _organizationService.AddUnit(unit);

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.Unit, unit.UnitId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created unit with id {unit.UnitId}");

            return(RedirectToAction(nameof(Units), new { id = unit.UnitId }));
        }
Esempio n. 7
0
        public async Task <IActionResult> ProcessSystemAccesses(List <int> systemAccessIds)
        {
            var    identity       = (ClaimsIdentity)User.Identity;
            int    employeeId     = Int32.Parse(identity.GetClaim("EmployeeId"));
            string employeeName   = identity.GetClaim(ClaimTypes.Name);
            var    systemAccesses = _systemService.GetSystemAccesses(systemAccessIds);

            foreach (var systemAccess in systemAccesses)
            {
                systemAccess.ProcessedById = employeeId;
                systemAccess.ProcessedOn   = DateTime.Now;
                await _auditLog.Append(employeeId, LogActionType.Process, LogResourceType.SystemAccess, systemAccess.SystemAccessId,
                                       $"{employeeName} processed system access with id {systemAccess.SystemAccessId}");
            }
            _systemService.SaveChanges();

            var systemAccessesByRequest = systemAccesses.GroupBy(s => s.Request, s => s).ToDictionary(g => g.Key, g => g.ToList());

            foreach (var request in systemAccessesByRequest.Keys)
            {
                string emailName = "RequestProcessed";
                var    model     = new
                {
                    _emailHelper.AppUrl,
                    _emailHelper.AppEmail,
                    Request        = request,
                    SystemAccesses = systemAccessesByRequest.GetValueOrDefault(request)
                };
                string subject    = _emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer);
                string receipient = request.RequestedBy.Email;
                _email.To(receipient)
                .Subject(subject)
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model)
                .Send();
            }
            return(RedirectToAction(nameof(MyProcessings)));
        }
Esempio n. 8
0
        public async Task <IActionResult> AddForm(Form form, List <int> systemIds, IFormFile uploadedFile)
        {
            var file = uploadedFile != null ? await saveFile(uploadedFile) : null;

            if (file != null)
            {
                form.FileId = file.FileId;
            }
            form = _formService.AddForm(form);
            if (systemIds.Count > 0)
            {
                foreach (var systemId in systemIds)
                {
                    form.Systems.Add(new SystemForm(systemId, form.FormId));
                }
                _formService.SaveChanges();
            }

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Create, LogResourceType.Form, form.FormId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} created form with id {form.FormId}");

            return(RedirectToAction(nameof(ViewForm), new { id = form.FormId }));
        }
Esempio n. 9
0
        public async Task <IActionResult> EditEmployee(int id, Employee update)
        {
            var employee = _userService.GetEmployee(id);

            var oldValue = JsonConvert.SerializeObject(employee, Formatting.Indented);

            _mapper.Map(update, employee);
            _userService.SaveChanges();
            var newValue = JsonConvert.SerializeObject(employee, Formatting.Indented);

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Update, LogResourceType.User, employee.EmployeeId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} updated user with id {employee.EmployeeId}", oldValue, newValue);

            return(RedirectToAction(nameof(ViewEmployee), new { id }));
        }
Esempio n. 10
0
        public async Task <IActionResult> RemoveEmployeeFromSupportUnit(int unitId, int employeeId)
        {
            var employee = _userService.GetEmployee(employeeId);

            if (employee.SupportUnitId == unitId)
            {
                employee.SupportUnitId = null;
                _userService.SaveChanges();

                var identity = (ClaimsIdentity)User.Identity;
                await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Update, LogResourceType.SupportUnit, unitId,
                                       $"{identity.GetClaim(ClaimTypes.Name)} removed employee {employeeId} from support unit {unitId}");
            }
            return(Ok());
        }
Esempio n. 11
0
        public async Task <IActionResult> Approve(int id, string password, string comments)
        {
            string username = ((ClaimsIdentity)User.Identity).GetClaim(ClaimTypes.NameIdentifier);

            if (!_adService.Authenticate(username, password))
            {
                RedirectToAction(nameof(EditReview), new { id });
            }

            Review review     = _requestService.GetReview(id);
            var    authResult = await _authService.AuthorizeAsync(User, review, "CanEnterReview");

            if (!authResult.Succeeded)
            {
                return(new ForbidResult());
            }

            Request request = _requestService.GetRequest(review.RequestId);

            review.Approve(comments);
            request.UpdatedOn = DateTime.Now;
            _requestService.SaveChanges();

            var identity = (ClaimsIdentity)User.Identity;
            await _auditLog.Append(identity.GetClaimAsInt("EmployeeId"), LogActionType.Approve, LogResourceType.Request, request.RequestId,
                                   $"{identity.GetClaim(ClaimTypes.Name)} approved request with id {request.RequestId}");

            if (review.ReviewOrder < request.Reviews.Count - 1)
            {
                Review nextReview = request.OrderedReviews[review.ReviewOrder + 1];
                string emailName  = "ReviewRequest";
                var    model      = new { _emailHelper.AppUrl, _emailHelper.AppEmail, Request = request };
                string subject    = _emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer);
                string receipient = nextReview.Reviewer.Email;
                _email.To(receipient)
                .Subject(subject)
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model)
                .Send();

                emailName  = "RequestUpdated";
                subject    = _emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer);
                receipient = request.RequestedBy.Email;
                _email.To(receipient)
                .Subject(subject)
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model)
                .Send();
            }
            else // last review
            {
                request.RequestStatus = RequestStatus.Approved;
                request.CompletedOn   = DateTime.Now;
                _requestService.SaveChanges();

                foreach (var requestedSystem in request.Systems)
                {
                    var systemAccess = new SystemAccess(request, requestedSystem);
                    _systemService.AddSystemAccess(systemAccess);
                }

                string emailName  = "RequestApproved";
                var    model      = new { _emailHelper.AppUrl, _emailHelper.AppEmail, Request = request };
                string subject    = _emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer);
                string receipient = request.RequestedBy.Email;
                _email.To(receipient)
                .Subject(subject)
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model)
                .Send();

                emailName = "ProcessRequest";
                _email.Subject(_emailHelper.GetSubjectFromTemplate(emailName, model, _email.Renderer))
                .UsingTemplateFromFile(_emailHelper.GetBodyTemplateFile(emailName), model);
                _email.Data.ToAddresses.Clear();
                var supportUnitIds = request.Systems.GroupBy(s => s.System.SupportUnitId, s => s).Select(g => g.Key).ToList();
                foreach (var supportUnitId in supportUnitIds)
                {
                    var supportUnit = _organizationService.GetSupportUnit((int)supportUnitId);
                    _email.To(supportUnit.Email);
                }
                await _email.SendAsync();
            }

            return(RedirectToAction(nameof(MyReviews)));
        }