public override string Uri(FileKey key, PathOptions options = null)
        {
            if (key == null || String.IsNullOrEmpty(key.ID))
            {
                return(null);
            }

            options             = options == null ? new PathOptions() : options;
            options.ForceDomain = Domain;

            var result = "";

            switch (key.FileType)
            {
            case FileType.Badge:
                options.ForceDomain = VoatSettings.Instance.SiteDomain;
                result = base.Uri(key, options);
                break;

            case FileType.Avatar:
            case FileType.Thumbnail:
                return(VoatUrlFormatter.BuildUrlPath(null, options, (new string[] { ContentPath(key), key.ID }).ToPathParts()));

                break;
            }
            return(result);
        }
Пример #2
0
        public void MiscPathTests()
        {
            var options = new PathOptions(true, true, "Somesite.com");

            string[] parts       = new string[] { "", " ", "white space ", "x.jpg" };
            var      expectedUrl = $"http{(VoatSettings.Instance.ForceHTTPS ? "s" : "")}://Somesite.com/white space/x.jpg";

            options.EscapeUrl = false;
            var url = VoatUrlFormatter.BuildUrlPath(null, options, parts);

            Assert.AreEqual(expectedUrl, url, "Condition:1.1");

            options.EscapeUrl = true;
            url = VoatUrlFormatter.BuildUrlPath(null, options, parts);

            Assert.AreEqual(Uri.EscapeUriString(expectedUrl), url, "Condition:1.2");

            options.EscapeUrl     = false;
            options.Normalization = Normalization.Lower;
            url = VoatUrlFormatter.BuildUrlPath(null, options, parts);
            Assert.AreEqual(expectedUrl.ToLower(), url, "Condition:1.3");

            options.EscapeUrl     = false;
            options.Normalization = Normalization.Upper;
            url = VoatUrlFormatter.BuildUrlPath(null, options, parts);
            Assert.AreEqual(expectedUrl.ToUpper(), url, "Condition:1.4");
        }
Пример #3
0
        protected override RuleOutcome EvaluateRule(VoatRuleContext context)
        {
            UserSubmission submission = context.PropertyBag.UserSubmission;

            switch (submission.Type)
            {
            case SubmissionType.Link:
                Data.Models.Submission recentlySubmitted = null;
                using (var repo = new Repository())
                {
                    recentlySubmitted = repo.FindSubverseLinkSubmission(context.Subverse.Name, submission.Url, TimeSpan.FromDays(15));
                }
                if (recentlySubmitted != null)
                {
                    string url = VoatUrlFormatter.BuildUrlPath(null, new Common.PathOptions()
                    {
                        FullyQualified = true, ProvideProtocol = true
                    }, $"v/{recentlySubmitted.Subverse}/{recentlySubmitted.ID}");

                    return(CreateOutcome(RuleResult.Denied, $"Sorry, this link has already been submitted recently. {url}"));
                }
                break;

            case SubmissionType.Text:

                //containsBannedDomain = BanningUtility.ContentContainsBannedDomain(context.Subverse.Name, submission.Content);
                break;
            }

            return(Allowed);
        }
        public async Task <ActionResult> AddModerator([Bind("ID,Subverse,UserName,Power")] SubverseModerator subverseAdmin)
        {
            if (!ModelState.IsValid)
            {
                return(View(subverseAdmin));
            }

            // check if caller can add mods, if not, deny posting
            if (!ModeratorPermission.HasPermission(User, subverseAdmin.Subverse, Domain.Models.ModeratorAction.InviteMods))
            {
                return(RedirectToAction("Index", "Home"));
            }

            subverseAdmin.UserName = subverseAdmin.UserName.TrimSafe();
            Subverse subverseModel = null;

            //lots of premature retuns so wrap the common code
            var sendFailureResult = new Func <string, ActionResult>(errorMessage =>
            {
                ViewBag.SubverseModel    = subverseModel;
                ViewBag.SubverseName     = subverseAdmin.Subverse;
                ViewBag.SelectedSubverse = string.Empty;
                ModelState.AddModelError(string.Empty, errorMessage);
                SetNavigationViewModel(subverseAdmin.Subverse);

                return(View("~/Views/Subverses/Admin/AddModerator.cshtml",
                            new SubverseModeratorViewModel
                {
                    UserName = subverseAdmin.UserName,
                    Power = subverseAdmin.Power
                }
                            ));
            });

            // prevent invites to the current moderator
            if (User.Identity.Name.IsEqual(subverseAdmin.UserName))
            {
                return(sendFailureResult("Can not add yourself as a moderator"));
            }

            string originalRecipientUserName = UserHelper.OriginalUsername(subverseAdmin.UserName);

            // prevent invites to the current moderator
            if (String.IsNullOrEmpty(originalRecipientUserName))
            {
                return(sendFailureResult("User can not be found"));
            }

            // get model for selected subverse
            subverseModel = DataCache.Subverse.Retrieve(subverseAdmin.Subverse);
            if (subverseModel == null)
            {
                return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound)));
            }

            if ((subverseAdmin.Power < 1 || subverseAdmin.Power > 4) && subverseAdmin.Power != 99)
            {
                return(sendFailureResult("Only powers levels 1 - 4 and 99 are supported currently"));
            }

            //check current mod level and invite level and ensure they are a lower level
            var currentModLevel = ModeratorPermission.Level(User, subverseModel.Name);

            if (subverseAdmin.Power <= (int)currentModLevel && currentModLevel != Domain.Models.ModeratorLevel.Owner)
            {
                return(sendFailureResult("Sorry, but you can only add moderators that are a lower level than yourself"));
            }

            int maximumOwnedSubs = VoatSettings.Instance.MaximumOwnedSubs;

            // check if the user being added is not already a moderator of 10 subverses
            var currentlyModerating = _db.SubverseModerator.Where(a => a.UserName == originalRecipientUserName).ToList();

            SubverseModeratorViewModel tmpModel;

            if (currentlyModerating.Count <= maximumOwnedSubs)
            {
                // check that user is not already moderating given subverse
                var isAlreadyModerator = _db.SubverseModerator.FirstOrDefault(a => a.UserName == originalRecipientUserName && a.Subverse == subverseAdmin.Subverse);

                if (isAlreadyModerator == null)
                {
                    // check if this user is already invited
                    var userModeratorInvitations = _db.ModeratorInvitation.Where(i => i.Recipient.ToLower() == originalRecipientUserName.ToLower() && i.Subverse.ToLower() == subverseModel.Name.ToLower());
                    if (userModeratorInvitations.Any())
                    {
                        return(sendFailureResult("Sorry, the user is already invited to moderate this subverse"));
                    }

                    // send a new moderator invitation
                    ModeratorInvitation modInv = new ModeratorInvitation
                    {
                        CreatedBy    = User.Identity.Name,
                        CreationDate = Repository.CurrentDate,
                        Recipient    = originalRecipientUserName,
                        Subverse     = subverseAdmin.Subverse,
                        Power        = subverseAdmin.Power
                    };

                    _db.ModeratorInvitation.Add(modInv);
                    _db.SaveChanges();
                    int invitationId   = modInv.ID;
                    var invitationBody = new StringBuilder();

                    //v/{subverse}/about/moderatorinvitations/accept/{invitationId}

                    string acceptInviteUrl = VoatUrlFormatter.BuildUrlPath(this.HttpContext, new PathOptions(true, true), $"/v/{subverseModel.Name}/about/moderatorinvitations/accept/{invitationId}");

                    invitationBody.Append("Hello,");
                    invitationBody.Append(Environment.NewLine);
                    invitationBody.Append($"@{User.Identity.Name} invited you to moderate v/" + subverseAdmin.Subverse + ".");
                    invitationBody.Append(Environment.NewLine);
                    invitationBody.Append(Environment.NewLine);
                    invitationBody.Append($"Please visit the following link if you want to accept this invitation: {acceptInviteUrl}");
                    invitationBody.Append(Environment.NewLine);
                    invitationBody.Append(Environment.NewLine);
                    invitationBody.Append("Thank you.");

                    var cmd = new SendMessageCommand(new Domain.Models.SendMessage()
                    {
                        Sender    = $"v/{subverseAdmin.Subverse}",
                        Recipient = originalRecipientUserName,
                        Subject   = $"v/{subverseAdmin.Subverse} moderator invitation",
                        Message   = invitationBody.ToString()
                    }, true).SetUserContext(User);
                    await cmd.Execute();

                    return(RedirectToAction("SubverseModerators"));
                }
                else
                {
                    return(sendFailureResult("Sorry, the user is already moderating this subverse"));
                }
            }
            else
            {
                return(sendFailureResult("Sorry, the user is already moderating a maximum of " + maximumOwnedSubs + " subverses"));
            }
        }