Esempio n. 1
0
        protected override void OnInitialized()
        {
            _clientRole = Utility.GetRole(AuthenticationStateTask ?? throw new ArgumentNullException()).Result;

            _newRule = new Infrastructure.Schema.Whitelist();

            if (_clientRole != UserRole.Roles.Privileged && _clientRole != UserRole.Roles.Administrator)
            {
                throw new Exception();
            }

            Log?fromLog = LogActionService.GetAndUnset();

            if (fromLog != null)
            {
                Console.WriteLine("From log: " + fromLog.Question);
                string pattern = @"(?:^|.+\.)" + fromLog.Question.Replace(".", @"\.") + "$";
                Rule().Pattern = pattern;
            }

            InitValidators();

            InitInputs();

            //

            _whitelistTable = new FlareTable <Infrastructure.Schema.Whitelist>(
                () => _clientRole == UserRole.Roles.Privileged ? WhitelistModel.List(ClientIP !) : WhitelistModel.List(),
                sessionStorage: SessionStorageService,
                identifier: "Rules.WhitelistTable", monospace: true);

            _whitelistTable.RegisterColumn(nameof(Infrastructure.Schema.Whitelist.ID));
            _whitelistTable.RegisterColumn(nameof(Infrastructure.Schema.Whitelist.Pattern));
            _whitelistTable.RegisterColumn(nameof(Infrastructure.Schema.Whitelist.Expires));
            _whitelistTable.RegisterColumn("_Edit", sortable: false, filterable: false, displayName: "", width: "56px");
            _whitelistTable.RegisterColumn("_Remove", sortable: false, filterable: false, displayName: "", width: "82px");

            _whitelistTable.OnRowClick += whitelist => { };

            BuildHeaders();
        }
Esempio n. 2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            logActionService = new LogActionService();
            LogViewModel          logModel         = new LogViewModel();
            int?                  targetId         = null;
            string                method           = "";
            string                ip               = "";
            string                fullname         = "";
            string                userCode         = "";
            string                targetName       = "";
            QuestionTempViewModel newQuestionModel = new QuestionTempViewModel();


            var user   = (UserViewModel)HttpContext.Current.Session["user"];
            int?userId = null;

            if (user != null)
            {
                userId = user.Id;
            }
            fullname = filterContext.HttpContext.User.Get(u => u.FullName);
            userCode = filterContext.HttpContext.User.Get(u => u.Code);
            //ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            string strHostName = "";

            strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

            IPAddress[] addr = ipEntry.AddressList;
            if (addr.Count() == 2)
            {
                ip = addr[1].ToString();
            }
            else
            {
                ip = addr[3].ToString();
            }



            if (IdParamName != null && filterContext.ActionParameters.ContainsKey(IdParamName))
            {
                targetId   = filterContext.ActionParameters[IdParamName] as Int32?;
                targetName = "Question";
            }
            if (TargetId != null && filterContext.ActionParameters.ContainsKey(TargetId))
            {
                targetId   = filterContext.ActionParameters[TargetId] as Int32?;
                targetName = "Import";
            }



            ILogActionService logger = new LogActionService();

            if (Action != null)
            {
                logModel.UserId     = userId;
                logModel.TargetId   = targetId;
                logModel.TargetName = targetName != null?targetName.ToString() : "";

                logModel.LogDate = DateTime.Now;
                logModel.Message = Message != null?Message.ToString() : "";

                logModel.Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                logModel.Action     = filterContext.ActionDescriptor.ActionName;
                logModel.Fullname   = fullname;
                logModel.UserCode   = userCode;
                logModel.Method     = Method != null?Method.ToString() : "";

                logModel.Ip = ip;
                logger.LogAction(logModel);
            }
        }
Esempio n. 3
0
        protected override void OnInitialized()
        {
            _clientRole = Utility.GetRole(AuthenticationStateTask ?? throw new ArgumentNullException()).Result;

            _newRule = new Infrastructure.Schema.Blacklist();

            Log?fromLog = LogActionService.GetAndUnset();

            if (fromLog != null)
            {
                Console.WriteLine("From log: " + fromLog.Question);
                string pattern = @"(?:^|.+\.)" + fromLog.Question.Replace(".", @"\.") + "$";
                _newRule.Pattern = pattern;
            }

            _validator = new Validator <ValidationResult>();

            _validator.Register("Pattern", () =>
            {
                if (_clientRole != UserRole.Roles.Administrator)
                {
                    return new[]
                    {
                        new Validation <ValidationResult>(ValidationResult.Warning,
                                                          "You are not permitted to create blacklist rules")
                    }
                }
                ;

                if (string.IsNullOrEmpty(Rule().Pattern))
                {
                    return new[] { new Validation <ValidationResult>(ValidationResult.Invalid, "Required") }
                }
                ;

                if (!Utility.ValidateRegex(Rule().Pattern))
                {
                    return new[] { new Validation <ValidationResult>(ValidationResult.Invalid, "Invalid regular expression") }
                }
                ;

                return(new[] { new Validation <ValidationResult>(ValidationResult.Valid, "Valid") });
            });

            _patternChangeDebouncer = new Debouncer <string>(pattern =>
            {
                _validator !.Validate();
                _onInputValidation.Trigger();
                InvokeAsync(StateHasChanged);
            }, "", 200);

            _validator.Validate();

            Task.Run(() =>
            {
                try
                {
                    BuildBlacklistTable();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            });

            //

            BuildHeaders();
        }