Example #1
0
        public ActionResult AddType(AddTypeViewModel permissionViewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    var permission = new Permission
                                         {
                                             Name = permissionViewModel.Name,
                                         };

                    _permissionService.Add(permission);
                    TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                    {
                        Message = "Permission Added",
                        MessageType = GenericMessages.success
                    };
                    unitOfWork.Commit();
                }
                catch(Exception ex)
                {
                    unitOfWork.Rollback();
                    LoggingService.Error(ex);
                    throw;
                }
            }

            return RedirectToAction("Index");
        }
Example #2
0
 public void Update(Permission item)
 {
     // Check there's not an object with same identifier already in context
     if (_context.Permission.Local.Select(x => x.Id == item.Id).Any())
     {
         throw new ApplicationException("Object already exists in context - you do not need to call Update. Save occurs on Commit");
     }
     _context.Entry(item).State = EntityState.Modified;
 }
Example #3
0
        /// <summary>
        ///  Delete permission and associated category permission for roles
        /// </summary>
        /// <param name="permission"></param>
        public void Delete(Permission permission)
        {
            var catPermForRoles = _categoryPermissionForRoleService.GetByPermission(permission.Id);
            foreach (var categoryPermissionForRole in catPermForRoles)
            {
                _categoryPermissionForRoleService.Delete(categoryPermissionForRole);
            }

            _context.Permission.Remove(permission);
        }
Example #4
0
        /// <summary>
        ///  Delete permission and associated category permission for roles
        /// </summary>
        /// <param name="permission"></param>
        public void Delete(Permission permission)
        {
            var catPermForRoles = _categoryPermissionForRoleRepository.GetByPermission(permission.Id);
            foreach (var categoryPermissionForRole in catPermForRoles)
            {
                _categoryPermissionForRoleRepository.Delete(categoryPermissionForRole);
            }

            _permissionRepository.Delete(permission);
        }
        public void Delete_Check_CategoryPermissionForRoles_Are_Deleted()
        {
            var permissionRepository = Substitute.For<IPermissionRepository>();
            var categoryPermissionForRoleRepository = Substitute.For<ICategoryPermissionForRoleRepository>();
            var permissionService = new PermissionService(permissionRepository, categoryPermissionForRoleRepository);

            var permission = new Permission { Name = "Ghost Rider", Id = Guid.NewGuid() };
            var catePermOne = new CategoryPermissionForRole {Id = Guid.NewGuid()};
            var catePermTwo = new CategoryPermissionForRole {Id = Guid.NewGuid()};
            var catepermrole = new List<CategoryPermissionForRole>{ catePermOne, catePermTwo };

            categoryPermissionForRoleRepository.GetByPermission(permission.Id).Returns(catepermrole);

            permissionService.Delete(permission);

            categoryPermissionForRoleRepository.Received().Delete(Arg.Is<CategoryPermissionForRole>(x => x.Id == catePermOne.Id));
            categoryPermissionForRoleRepository.Received().Delete(Arg.Is<CategoryPermissionForRole>(x => x.Id == catePermTwo.Id));
        }
Example #6
0
 /// <summary>
 /// Add a new permission
 /// </summary>
 /// <param name="permission"></param>
 public Permission Add(Permission permission)
 {
     permission.Name = StringUtils.SafePlainText(permission.Name);
     return _context.Permission.Add(permission);
 }
Example #7
0
 /// <summary>
 /// Add a new permission
 /// </summary>
 /// <param name="permission"></param>
 public void Add(Permission permission)
 {
     permission.Name = StringUtils.SafePlainText(permission.Name);
     _permissionRepository.Add(permission);
 }
        private InstallerResult CreateInitialData()
        {
            var installerResult = new InstallerResult { Successful = true, Message = "Congratulations, MVC Forum has installed successfully" };

            // I think this is all I need to call to kick EF into life
            //EFCachingProviderConfiguration.DefaultCache = new AspNetCache();
            //EFCachingProviderConfiguration.DefaultCachingPolicy = CachingPolicy.CacheAll;

            // Now setup the services as we can't do it in the constructor
            InitialiseServices();

            // First UOW to create the data needed for other saves
            using (var unitOfWork = _UnitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    // Check if category exists or not, we only do a single check for the first object within this
                    // UOW because, if anything failed inside. Everything else would be rolled back to because of the 
                    // transaction
                    const string exampleCatName = "Example Category";
                    if (_categoryService.GetAll().FirstOrDefault(x => x.Name == exampleCatName) == null)
                    {
                        // Doesn't exist so add the example category
                        var exampleCat = new Category { Name = exampleCatName, ModeratePosts = false, ModerateTopics = false};
                        _categoryService.Add(exampleCat);

                        // Add the default roles
                        var standardRole = new MembershipRole { RoleName = "Standard Members" };
                        var guestRole = new MembershipRole { RoleName = "Guest" };
                        var moderatorRole = new MembershipRole { RoleName = "Moderator" };
                        var adminRole = new MembershipRole { RoleName = "Admin" };
                        _roleService.CreateRole(standardRole);
                        _roleService.CreateRole(guestRole);
                        _roleService.CreateRole(moderatorRole);
                        _roleService.CreateRole(adminRole);

                        unitOfWork.Commit();
                    }

                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    installerResult.Exception = ex.InnerException;
                    installerResult.Message = "Error creating the initial data >> Category & Roles";
                    installerResult.Successful = false;
                    return installerResult;
                }
            }

            // Add / Update the default language strings
            installerResult = AddOrUpdateTheDefaultLanguageStrings(installerResult);
            if (!installerResult.Successful)
            {
                return installerResult;
            }   

            // Now we have saved the above we can create the rest of the data
            using (var unitOfWork = _UnitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    // if the settings already exist then do nothing
                    if (_settingsService.GetSettings(false) == null)
                    {
                        // Get the default language
                        var startingLanguage = _localizationService.GetLanguageByName("en-GB");

                        // Get the Standard Members role
                        var startingRole = _roleService.GetRole("Standard Members");

                        // create the settings
                        var settings = new Settings
                        {
                            ForumName = "MVC Forum",
                            ForumUrl = "http://www.mydomain.com",
                            IsClosed = false,
                            EnableRSSFeeds = true,
                            DisplayEditedBy = true,
                            EnablePostFileAttachments = false,
                            EnableMarkAsSolution = true,
                            EnableSpamReporting = true,
                            EnableMemberReporting = true,
                            EnableEmailSubscriptions = true,
                            ManuallyAuthoriseNewMembers = false,
                            EmailAdminOnNewMemberSignUp = true,
                            TopicsPerPage = 20,
                            PostsPerPage = 20,
                            EnablePrivateMessages = true,
                            MaxPrivateMessagesPerMember = 50,
                            PrivateMessageFloodControl = 1,
                            EnableSignatures = false,
                            EnablePoints = true,
                            PointsAllowedToVoteAmount = 1,
                            PointsAddedPerPost = 1,
                            PointsAddedForSolution = 4,
                            PointsDeductedNagativeVote = 2,
                            AdminEmailAddress = "*****@*****.**",
                            NotificationReplyEmail = "*****@*****.**",
                            SMTPEnableSSL = false,
                            Theme = "Metro",
                            NewMemberStartingRole = startingRole,
                            DefaultLanguage = startingLanguage,
                            ActivitiesPerPage = 20,
                            EnableAkisment = false,
                            EnableSocialLogins = false,
                            EnablePolls = true
                        };
                        _settingsService.Add(settings);

                        unitOfWork.Commit();
                    }
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    installerResult.Exception = ex.InnerException;
                    installerResult.Message = "Error creating the initial data >> Settings";
                    installerResult.Successful = false;
                    return installerResult;
                }
            }


            // Now we have saved the above we can create the rest of the data
            using (var unitOfWork = _UnitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    // If the admin user exists then don't do anything else
                    if (_membershipService.GetUser("admin") == null)
                    {
                        // Set up the initial permissions
                        var readOnly = new Permission { Name = "Read Only" };
                        var deletePosts = new Permission { Name = "Delete Posts" };
                        var editPosts = new Permission { Name = "Edit Posts" };
                        var stickyTopics = new Permission { Name = "Sticky Topics" };
                        var lockTopics = new Permission { Name = "Lock Topics" };
                        var voteInPolls = new Permission { Name = "Vote In Polls" };
                        var createPolls = new Permission { Name = "Create Polls" };
                        var createTopics = new Permission { Name = "Create Topics" };
                        var attachFiles = new Permission { Name = "Attach Files" };
                        var denyAccess = new Permission { Name = "Deny Access" };

                        _permissionService.Add(readOnly);
                        _permissionService.Add(deletePosts);
                        _permissionService.Add(editPosts);
                        _permissionService.Add(stickyTopics);
                        _permissionService.Add(lockTopics);
                        _permissionService.Add(voteInPolls);
                        _permissionService.Add(createPolls);
                        _permissionService.Add(createTopics);
                        _permissionService.Add(attachFiles);
                        _permissionService.Add(denyAccess);

                        // create the admin user and put him in the admin role
                        var admin = new MembershipUser
                        {
                            Email = "*****@*****.**",
                            UserName = "******",
                            Password = "******",
                            IsApproved = true,
                            DisableEmailNotifications = false,
                            DisablePosting = false,
                            DisablePrivateMessages = false
                        };
                        _membershipService.CreateUser(admin);

                        // Do a save changes just in case
                        unitOfWork.SaveChanges();

                        // Put the admin in the admin role
                        var adminRole = _roleService.GetRole("Admin");
                        admin.Roles = new List<MembershipRole> { adminRole };

                        unitOfWork.Commit();
                    }
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    installerResult.Exception = ex.InnerException;
                    installerResult.Message = "Error creating the initial data >> Admin user & Permissions";
                    installerResult.Successful = false;
                    return installerResult;
                }
            }

            // Do this so search works and doesn't create a null reference.
            _luceneService.UpdateIndex();

            return installerResult;
        }
Example #9
0
 public void Delete(Permission item)
 {
     _context.Permission.Remove(item);
 }
Example #10
0
 public Permission Add(Permission permission)
 {
     return _context.Permission.Add(permission);
 }