Beispiel #1
0
        public ActionResult History(Guid id)
        {
            using (var db = ApplicationDbContext.Create())
            {
                //var record = GetRecord(id, db);
                var file = GetFile(id, db);

                EnsureUserIsAllowed(file.CatalogRecord, db);

                var events = db.Events
                             .Where(x => x.RelatedManagedFiles.Any(f => f.Id == id))
                             .OrderByDescending(x => x.Timestamp)
                             .Include(x => x.RelatedCatalogRecord)
                             .Include(x => x.RelatedManagedFiles)
                             .Include(x => x.User);

                var model = new ManagedFileHistoryModel();
                model.File           = file;
                model.IsUserCurator  = true;
                model.IsUserApprover = true;

                var logs = new List <HistoryEventModel>();

                foreach (var log in events)
                {
                    var eventModel = HistoryEventModel.FromEvent(log, log.User);
                    logs.Add(eventModel);
                }

                // Sort all the events.
                var sorted = logs.OrderByDescending(x => x.Timestamp);
                foreach (var log in sorted)
                {
                    model.Events.Add(log);
                }

                return(View(model));
            }
        }
Beispiel #2
0
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                throw new HttpException(400, "Bad Request");
            }

            using (var db = ApplicationDbContext.Create())
            {
                var user = db.Users
                           .Where(x => x.UserName == id)
                           .Include(x => x.Organizations)
                           .Include(x => x.AuthorFor)
                           .Include(x => x.CuratorFor)
                           .Include(x => x.ApproverFor)
                           .FirstOrDefault();
                if (user == null)
                {
                    throw new HttpException(404, "Not Found");
                }

                // Information about the requesting user.
                var thisUser = db.Users.Where(x => x.UserName == User.Identity.Name)
                               .Include(x => x.Organizations)
                               .FirstOrDefault();


                var model = new UserDetailsModel();
                model.User = user;

                // Get information about each organization the user belongs to.
                foreach (var o in user.Organizations)
                {
                    var orgModel = new UserInOrganizationModel();
                    orgModel.OrganizationId   = o.Id;
                    orgModel.OrganizationName = o.Name;
                    model.Organizations.Add(orgModel);

                    // TODO better to do this in one query above.
                    var permissions = db.Permissions
                                      .Where(x => x.User.Id == user.Id && x.Organization.Id == o.Id);

                    foreach (var permission in permissions)
                    {
                        switch (permission.Right)
                        {
                        case Right.CanAssignRights:
                            orgModel.CanAssignRights = true;
                            break;

                        case Right.CanViewAllCatalogRecords:
                            orgModel.CanViewAllCatalogRecords = true;
                            break;

                        case Right.CanAssignCurator:
                            orgModel.CanAssignCurators = true;
                            break;

                        default:
                            break;
                        }
                    }
                }

                // Get history information for the user.
                var events = db.Events
                             .Where(x => x.User.UserName == id)
                             .OrderByDescending(x => x.Timestamp)
                             .Include(x => x.RelatedCatalogRecord)
                             .Include(x => x.RelatedManagedFiles);

                foreach (var userEvent in events)
                {
                    var eventModel = HistoryEventModel.FromEvent(userEvent, user);
                    model.Events.Add(eventModel);
                }

                // Ideas for more events to add
                // TODO Show when this user was created?
                // TODO Show when this user creates other users?
                // TODO Show when records, files, and anything else is edited?

                // Can the requesting user edit the user?
                var org = OrganizationHelper.GetOrganizationByHost(Request, db);
                if (org == null)
                {
                    model.IsOrganizationAmbiguous = true;
                }

                bool isOrgAdmin = false;
                if (org != null)
                {
                    isOrgAdmin = OrganizationHelper.DoesUserHaveRight(db, User, org.Id, Right.CanAssignRights);
                }

                model.CanEditUser = thisUser.IsAdministrator ||
                                    isOrgAdmin ||
                                    thisUser.UserName == id;

                // Permissions
                model.CanEditPermissions             = thisUser.IsAdministrator || isOrgAdmin;
                model.IsEditingUserSiteAdministrator = thisUser.IsAdministrator;

                if (org != null)
                {
                    model.OrganizationName = org.Name;
                }
                else
                {
                    model.OrganizationName = string.Join(", ", user.Organizations.Select(x => x.Name));
                }

                model.IsSiteAdministrator = user.IsAdministrator;

                if (org != null)
                {
                    var orgPermissions = user.Permissions.Where(x => x.Organization.Id == org.Id);
                    model.CanAssignRights          = orgPermissions.Any(x => x.Right == Right.CanAssignRights);
                    model.CanViewAllCatalogRecords = orgPermissions.Any(x => x.Right == Right.CanViewAllCatalogRecords);
                    model.CanAssignCurator         = orgPermissions.Any(x => x.Right == Right.CanAssignCurator);
                    model.CanEditOrganization      = orgPermissions.Any(x => x.Right == Right.CanEditOrganization);
                    model.CanApprove = orgPermissions.Any(x => x.Right == Right.CanApprove);
                }

                // Map information from the user object to the view model.
                model.UserName           = user.UserName;
                model.FirstName          = user.FirstName;
                model.LastName           = user.LastName;
                model.Affiliation        = user.Affiliation;
                model.ContactInformation = user.ContactInformation;
                model.Orcid       = user.Orcid;
                model.Email       = user.Email;
                model.PhoneNumber = user.PhoneNumber;


                return(View(model));
            }
        }