public bool UserHasEditPermission(User user, Document document)
        {
            if (document.State != DocumentState.Working) return false;

            if (document.WorkMode == DocumentWorkMode.Private.ID)
            {
                if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();
                if (document.Author.UserID == user.UserID) return true;
                return false;
            }
            if (document.WorkMode == DocumentWorkMode.Collaborative.ID)
            {
                if (!document.AuthorReference.IsLoaded) document.AuthorReference.Load();

                var authorTeam = _userService.GetUserTeam(document.Author);
                var userTeam = _userService.GetUserTeam(user);

                if (authorTeam == null) return false;
                if (userTeam == null) return false;

                if (authorTeam.TeamID == userTeam.TeamID) return true;
            }

            return false;
        }
 public void AddToWorkshop(int workshopId, User user)
 {
     Workshop workshop = _workshopRepository.GetByID(workshopId);
     user.UserLevel = (int) UserLevels.WorkshopMember;
     user.Workshop = workshop;
     if (user.EntityState == System.Data.EntityState.Detached) _userRepository.Add(user);
     _userRepository.Save();
 }
        public void MapToModel(UserEntityViewModel entityViewModel, User model, bool passwordChanged)
        {
            model.FirstName = entityViewModel.Firstname.CapFirstLetter();
            model.LastName = entityViewModel.Lastname.CapFirstLetter();
            model.Email = entityViewModel.Email.ToLower();
            model.Username = entityViewModel.Username.ToLower();
            model.UserLevel = entityViewModel.UserLevel;

            if (passwordChanged)
            {
                model.PasswordSalt = _cryptoService.CreateSalt();
                model.PasswordHash = _cryptoService.GetPasswordHash(entityViewModel.Password, model.PasswordSalt);
            }
        }
 public void ChangeDirector(User newDirector)
 {
     User currentDirector = null;
     var directors = _userRepository.GetByUserLevel((int) UserLevels.Director);
     if (directors.Length > 0) currentDirector = directors[0];
     if(currentDirector!=null)
     {
         currentDirector.UserLevel = (int) UserLevels.Unassigned;
         _userRepository.Save();
     }
     newDirector.UserLevel = (int) UserLevels.Director;
     if(newDirector.EntityState==System.Data.EntityState.Detached) _userRepository.Add(newDirector);
     _userRepository.Save();
 }
        public WorkflowRelatedStatus GetUserWorkflowRelatedStatus(User user, Document document)
        {
            //check if document was previously approved
            if (document.State == DocumentState.Finalized) return WorkflowRelatedStatus.NotAllowed;

            if (user.UserLevel == (int)UserLevels.Unassigned || user.UserLevel == (int)UserLevels.Administrator) return WorkflowRelatedStatus.NotAllowed;

            if (user.UserLevel == (int)UserLevels.Director)
            {
                if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();

                foreach (var item in document.DocumentReviewQueueItems)
                {
                    if (item.ForDirector) return WorkflowRelatedStatus.HighestDecisionMaker;
                }
            }

            //if user is part of management team or is a director, must check the fields ForManagement or ForUser
            //in DocumentReviewQueueItems collection
            if (user.UserLevel == (int)UserLevels.ManagementTeamMember)
            {
                if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();

                if (document.DocumentReviewQueueItems.Any(item => item.ForManagement))
                {
                    return WorkflowRelatedStatus.DecisionMaker;
                }
            }

            if (!user.DocumentReviewQueueItems.IsLoaded) user.DocumentReviewQueueItems.Load();
            foreach (var item in user.DocumentReviewQueueItems)
            {
                if (!item.DocumentReference.IsLoaded) item.DocumentReference.Load();
                if (item.Document.DocumentID == document.DocumentID) return WorkflowRelatedStatus.DecisionMaker;
            }

            //check if current user is author
            //first check if the document is not already on a workflow
            if (!document.DocumentReviewQueueItems.IsLoaded) document.DocumentReviewQueueItems.Load();
            if (document.DocumentReviewQueueItems.Count == 0)
            {
                if (document.Author.UserID == user.UserID)
                    return (user.UserLevel == (int)UserLevels.Director
                                ? WorkflowRelatedStatus.DirectorAuthor
                                : WorkflowRelatedStatus.Author);
            }

            return WorkflowRelatedStatus.NotAllowed;
        }
        public UserEntityViewModel MapToViewModel(User model)
        {
            var viewModel = new UserEntityViewModel();
            if (model != null)
            {
                viewModel.Id = model.UserID;
                viewModel.Email = model.Email;
                viewModel.Firstname = model.FirstName;
                viewModel.Lastname = model.LastName;
                viewModel.UserLevel = model.UserLevel;
                viewModel.Username = model.Username;
            }

            return viewModel;
        }
 public UserDetailsDisplay(User user)
 {
     Id = user.UserID;
     Fullname = user.Fullname;
     Email = user.Email;
     switch (user.UserLevel)
     {
         case (int)UserLevels.Unassigned:
             RoleMessage =
                 "This user is currently unassigned. Assign him or her through the Administration Panel.";
             break;
         case (int)UserLevels.WorkshopMember:
             OrganizationName = user.Workshop.Name;
             OrganizationId = user.Workshop.WorkshopID;
             OrganizationLink = RoutesDictionary.WorkshopDetailsRoute((int)OrganizationId);
             RoleMessage = String.Format("This user is a member of the {0} workshop", OrganizationName);
             break;
         case (int)UserLevels.WorkshopManager:
             if (!user.ManagesWorkshopReference.IsLoaded) user.ManagesWorkshopReference.Load();
             OrganizationName = user.ManagesWorkshop.Name;
             OrganizationId = user.ManagesWorkshop.WorkshopID;
             OrganizationLink = RoutesDictionary.WorkshopDetailsRoute((int)OrganizationId);
             RoleMessage = String.Format("This user is the manager of the {0} workshop", OrganizationName);
             break;
         case (int)UserLevels.TeamManager:
             if (!user.ManagesTeamReference.IsLoaded) user.ManagesTeamReference.Load();
             OrganizationName = user.ManagesTeam.Name;
             OrganizationId = user.ManagesTeam.TeamID;
             OrganizationLink = RoutesDictionary.TeamDetailsRoute((int)OrganizationId);
             RoleMessage = String.Format("This user is the manager of the {0} team", OrganizationName);
             break;
         case (int)UserLevels.ManagementTeamMember:
             OrganizationName = "Management team";
             OrganizationId = null;
             OrganizationLink = RoutesDictionary.ManagementTeamRoute;
             RoleMessage = "This user is part of the management team.";
             break;
         case (int)UserLevels.Director:
             RoleMessage = "This user is the current director.";
             break;
         case (int)UserLevels.Administrator:
             RoleMessage = "This user is an administrator.";
             break;
         default:
             RoleMessage = string.Empty;
             break;
     }
 }
        public User AddFirstAdmin(string password)
        {
            var admin = new User
                             {
                                 UserLevel = (int) UserLevels.Administrator,
                                 Username = "******",
                                 PasswordSalt = _cryptographerSerivce.CreateSalt()
                             };
            admin.PasswordHash = _cryptographerSerivce.GetPasswordHash(password, admin.PasswordSalt);
            admin.FirstName = string.Empty;
            admin.LastName = string.Empty;
            admin.Email = string.Empty;

            SaveUser(admin);

            return admin;
        }
        public void ChangeTeamManager(int teamId, User newManager)
        {
            Team team = _teamRepository.GetByID(teamId);
            User currentManager = team.Manager;

            newManager.UserLevel = (int) UserLevels.TeamManager;

            if (newManager.EntityState == System.Data.EntityState.Detached) _userRepository.Add(newManager);
            _userRepository.Save();

            team.Manager = newManager;
            _teamRepository.Save();
            if (currentManager != null)
            {
                currentManager.UserLevel = (int)UserLevels.Unassigned;
                currentManager.ManagesTeam = null;
                _userRepository.Save();
            }
        }
 public void SaveUser(User user)
 {
     if (user.EntityState == System.Data.EntityState.Detached) _userRepository.Add(user);
     _userRepository.Save();
 }
 public void MakeWorkflowDecision(User user, Document document, WorkflowDecisions decision, string message)
 {
     var workflowItem = AddWorkflowItem(document, user, decision, message);
     ClearDocumentReviewQueue(document);
     switch (decision)
     {
         case WorkflowDecisions.Accept:
             document.ApprovedBy = user;
             document.ApprovalDate = DateTime.Now;
             document.State = DocumentState.Finalized;
             break;
         case WorkflowDecisions.Reject:
             document.State = DocumentState.Working;
             break;
         case WorkflowDecisions.SendToSuperior:
             document.State = DocumentState.InReview;
             AddDocumentReviewQueueItem(user, document, workflowItem);
             break;
     }
     SaveDocument(document);
 }
 //gets the user's workshop, if the user is a workshop member or workshop manager
 //otherwise returns null
 public Workshop GetUserWorkshop(User user)
 {
     switch (user.UserLevel)
     {
         case (int) UserLevels.WorkshopMember:
             if (!user.WorkshopReference.IsLoaded) user.WorkshopReference.Load();
             return user.Workshop;
         case (int) UserLevels.WorkshopManager:
             if (!user.ManagesWorkshopReference.IsLoaded) user.ManagesWorkshopReference.Load();
             return user.ManagesWorkshop;
         default:
             return null;
     }
 }
        public void RemoveManagementTeamMember(User user)
        {
            user.UserLevel = (int) UserLevels.Unassigned;
            user.ManagesTeam = null;
            user.ManagesWorkshop = null;

            SaveUser(user);
        }
 public Document[] GetAuthorDocuments(User currentUser)
 {
     return _documentRepository.GetByAuthor(currentUser);
 }
 //gets the user's team, if the user is a workshop member, workshop manager or team manager
 //otherwise returns null
 public Team GetUserTeam(User user)
 {
     switch (user.UserLevel)
     {
         case (int) UserLevels.WorkshopMember:
             if (!user.WorkshopReference.IsLoaded) user.WorkshopReference.Load();
             var workshop1 = user.Workshop;
             if (!workshop1.TeamReference.IsLoaded) workshop1.TeamReference.Load();
             return workshop1.Team;
             break;
         case (int) UserLevels.WorkshopManager:
             if (!user.ManagesWorkshopReference.IsLoaded) user.ManagesWorkshopReference.Load();
             var workshop2 = user.ManagesWorkshop;
             if (!workshop2.TeamReference.IsLoaded) workshop2.TeamReference.Load();
             return workshop2.Team;
             break;
         case (int) UserLevels.TeamManager:
             if (!user.ManagesTeamReference.IsLoaded) user.ManagesTeamReference.Load();
             return user.ManagesTeam;
             break;
         default:
             return null;
     }
 }
        public Document[] GetPublicDocumentsByTeam(User currentUser, Team team)
        {
            var teamUsers = _administrationService.GetAllUsersInsideTeam(team);
            var currentUserTeam = _userService.GetUserTeam(currentUser);
            var documents = new List<Document>();
            foreach (var user in teamUsers)
            {
                Func<Document, bool> documentSelectionCondition;
                if(currentUserTeam.TeamID == team.TeamID)
                {
                    documentSelectionCondition = (d => d.DistributionMode == DocumentDitributionMode.Public.ID ||
                                                         d.DistributionMode == DocumentDitributionMode.Team.ID);
                }
                else
                {
                    documentSelectionCondition = (d => d.DistributionMode == DocumentDitributionMode.Public.ID);
                }

                if(!user.DocumentsAuthored.IsLoaded) user.DocumentsAuthored.Load();
                documents.AddRange(user.DocumentsAuthored.Where(documentSelectionCondition).ToArray());
            }

            return documents.ToArray();
        }
 public void SaveUserViewModel(int? userId, UserEntityViewModel userEntityViewModel, EditModes editMode)
 {
     _userValidator.EntityViewModel = userEntityViewModel;
     bool passwordChanged = !String.IsNullOrEmpty(userEntityViewModel.Password);
     bool isValid = false;
     switch (editMode)
     {
         case EditModes.Insert:
             isValid = _userValidator.IsInsertValid();
             break;
         case EditModes.Update:
             isValid = _userValidator.IsUpdateValid(passwordChanged);
             break;
     }
     if (isValid)
     {
         User user = null;
         if (userId == null) user = new User();
         else user = _userRepository.GetByID((int) userId);
         if(user == null) throw new EntityException("User does not exist.");
         _userMapper.MapToModel(userEntityViewModel, user, passwordChanged);
         switch (userEntityViewModel.UserInsertScenario)
         {
             case UserInsertScenario.WorkshopMember:
                 _userService.AddToWorkshop((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.WorkshopManager:
                 _userService.ChangeWorkshopManager((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.TeamManager:
                 _userService.ChangeTeamManager((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.Manager:
                 _userService.AddToManagementTeam(user);
                 break;
             case UserInsertScenario.Director:
                 _userService.ChangeDirector(user);
                 break;
             case UserInsertScenario.Unassigned:
             default:
                 _userService.SaveUser(user);
                 break;
         }
     }
     else
     {
         throw new ValidationException();
     }
 }
        private void AddDocumentReviewQueueItem(User user, Document document, WorkflowItem workflowItem)
        {
            DocumentReviewQueueItem reviewQueueItem = new DocumentReviewQueueItem();

            if (user.UserLevel == (int)UserLevels.TeamManager)
            {
                reviewQueueItem.ForManagement = true;
                reviewQueueItem.ForDirector = false;
                reviewQueueItem.User = null;
            }
            else if (user.UserLevel == (int)UserLevels.ManagementTeamMember)
            {
                reviewQueueItem.ForManagement = false;
                reviewQueueItem.ForDirector = true;
                reviewQueueItem.User = null;
            }
            else
            {
                reviewQueueItem.ForManagement = false;
                reviewQueueItem.ForDirector = false;
                reviewQueueItem.User = _userService.GetUserSuperior(user);
            }

            reviewQueueItem.WorkflowItem = workflowItem;
            document.DocumentReviewQueueItems.Add(reviewQueueItem);
        }
 public void AddToManagementTeam(User user)
 {
     user.UserLevel = (int) UserLevels.ManagementTeamMember;
     if (user.EntityState == System.Data.EntityState.Detached) _userRepository.Add(user);
     _userRepository.Save();
 }
 public void MapToModel(UserEntityViewModel entityViewModel, User model)
 {
     MapToModel(entityViewModel, model, true);
 }
 public Document[] GetDocumentsThatNeedWorkflowDecision(User user)
 {
     if(user.UserLevel==(int)UserLevels.ManagementTeamMember)
     {
         return _documentRepository.GetApprovalDocumentsForManagement();
     }
     else if (user.UserLevel==(int)UserLevels.Director)
     {
         return _documentRepository.GetApprovalDocumentsForDirector();
     }
     else
     {
         return _documentRepository.GetApprovalDocumentsForUser(user.UserID);
     }
 }
 public Document CreateDocument(User currentUser)
 {
     throw new NotImplementedException();
 }
 public void Delete(User item)
 {
     Context.DeleteObject(item);
 }
 public void Add(User item)
 {
     Context.AddToUsers(item);
 }
        public Document[] GetPublicDocumentsByWorkshop(User currentUser, Workshop workshop)
        {
            var workshopUsers = _administrationService.GetAllUsersInsideWorkshop(workshop);
            var currentUserTeam = _userService.GetUserTeam(currentUser);
            var currentUserWorkshop = _userService.GetUserWorkshop(currentUser);
            if(!workshop.TeamReference.IsLoaded) workshop.TeamReference.Load();
            var documents = new List<Document>();
            foreach (var user in workshopUsers)
            {
                Func<Document, bool> documentSelectionCondition;
                if(currentUserWorkshop.WorkshopID==workshop.WorkshopID)
                {
                    documentSelectionCondition = (d => d.DistributionMode == DocumentDitributionMode.Protected.ID ||
                                                       d.DistributionMode == DocumentDitributionMode.Public.ID ||
                                                       d.DistributionMode == DocumentDitributionMode.Team.ID);
                }
                else if (currentUserTeam.TeamID == workshop.Team.TeamID)
                {
                    documentSelectionCondition = (d => d.DistributionMode == DocumentDitributionMode.Public.ID ||
                                                         d.DistributionMode == DocumentDitributionMode.Team.ID);
                }
                else
                {
                    documentSelectionCondition = (d => d.DistributionMode == DocumentDitributionMode.Public.ID);
                }

                if(!user.DocumentsAuthored.IsLoaded) user.DocumentsAuthored.Load();
                documents.AddRange(user.DocumentsAuthored.Where(documentSelectionCondition).ToArray());
            }

            return documents.ToArray();
        }
        private void SaveAndCreateManager(TeamEntityViewModel teamEntityViewModel)
        {
            _teamValidator.EntityViewModel = teamEntityViewModel;
            var managerViewModel = teamEntityViewModel.ManagerEntityViewModel;
            _userValidator.EntityViewModel = managerViewModel;
            bool isTeamValid = _teamValidator.IsInsertValid();
            bool isUserValid = _userValidator.IsInsertValid();
            bool isValid = isTeamValid && isUserValid;
            if(isValid)
            {
                var teamManager = new User();
                _userMapper.MapToModel(managerViewModel, teamManager);

                var team = new Team();
                _teamMapper.MapToModel(teamEntityViewModel, team, false);

                teamManager.UserLevel = (int) UserLevels.TeamManager;
                teamManager.ManagesTeam = team;
                team.Manager = teamManager;

                _teamRepository.Add(team);
                _teamRepository.Save();
            }
            else throw new ValidationException();
        }
 public void ChangeWorkshopManager(int workshopId, User newManager)
 {
     Workshop workshop = _workshopRepository.GetByID(workshopId);
     User currentManager = workshop.Manager;
     if(currentManager!=null)
     {
         currentManager.UserLevel = (int) UserLevels.Unassigned;
         currentManager.ManagesWorkshop = null;
         _userRepository.Save();
     }
     newManager.UserLevel = (int) UserLevels.WorkshopManager;
     newManager.ManagesWorkshop = workshop;
     workshop.Manager = newManager;
     if (newManager.EntityState == System.Data.EntityState.Detached) _userRepository.Add(newManager);
     _userRepository.Save();
 }
 public User GetUserSuperior(User user)
 {
     switch (user.UserLevel)
     {
         case (int)UserLevels.Unassigned:
         case (int)UserLevels.Administrator:
         case (int)UserLevels.Director:
         case (int)UserLevels.TeamManager://no direct superior, can be anyone from management team
             return null;
         case (int)UserLevels.ManagementTeamMember:
             return _userRepository.GetDirector();
         case (int)UserLevels.WorkshopMember:
             if (!user.WorkshopReference.IsLoaded) user.WorkshopReference.Load();
             if (!user.Workshop.ManagerReference.IsLoaded) user.Workshop.ManagerReference.Load();
             return user.Workshop.Manager;
         case (int)UserLevels.WorkshopManager:
             if (!user.ManagesWorkshopReference.IsLoaded) user.ManagesWorkshopReference.Load();
             if (!user.ManagesWorkshop.TeamReference.IsLoaded) user.ManagesWorkshop.TeamReference.Load();
             if (!user.ManagesWorkshop.Team.ManagerReference.IsLoaded) user.ManagesWorkshop.Team.ManagerReference.Load();
             return user.ManagesWorkshop.Team.Manager;
     }
     return null;
 }
        private UserEntityViewModel MapToViewModel(User model)
        {
            if(model == null) model = new User();
            var newUserViewModel = _userMapper.MapToViewModel(model);

            return newUserViewModel;
        }
        private WorkflowItem AddWorkflowItem(Document document, User user, WorkflowDecisions workflowDecision, string message)
        {
            WorkflowItem workflowItem = new WorkflowItem();
            workflowItem.Decision = (int)workflowDecision;
            workflowItem.DecisionDate = DateTime.Now;
            workflowItem.Document = document;
            workflowItem.Message = message;
            workflowItem.User = user;

            return workflowItem;
        }