public ActionResult Create(FormCollection collection) { var userId = User.Identity.GetUserId(); var loggedInUser = userRepository.GetById(Convert.ToInt32(userId)); if (loggedInUser.Role == "admin") { try { var debtors = collection["Debtors[]"].Split(','); var urls = collection["Urls[]"].Split(','); var user = new User(); user.SetCredentials(collection["Username"], collection["Password"]); user.ApiKey = collection["ApiKey"]; user.AllowedIP = collection["AllowedIP"]; var role = (Role)Convert.ToInt32(collection["UserRole"]); user.Role = role.ToString(); if (collection["Email"] != null) { user.Email = collection["Email"]; } else { user.Email = ""; } foreach (string debtorId in debtors) { if (!String.IsNullOrEmpty(debtorId)) { var parts = debtorId.Split(' '); var debtor = debtorRepository.GetById(parts[0]); user.Debtors.Add(debtor); } } foreach (string urlName in urls) { if (!String.IsNullOrEmpty(urlName)) { var url = urlRepository.GetByName(urlName); user.Urls.Add(url); } } userRepository.Insert(user); return(RedirectToAction("Index")); } catch (Exception e) { return(View()); } } else { throw new Exception("You are not allowed to access this"); } }
public IHttpActionResult Edit(UserViewModel data) { try { var user = userRepository.GetById(data.Id); user.Username = data.Username; user.DisplayName = data.DisplayName; user.Email = data.Email; user.Apikey = data.Apikey; user.Role = data.Role; user.AllowedIP = data.AllowedIP; user.Enabled = data.Enabled; user.Urls.Clear(); foreach (var urlItem in data.Urls) { var url = urlRepository.GetByName(urlItem.Name); user.Urls.Add(url); } userRepository.Update(user); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public static List <Log> ListByUrlAndStatus(DateTime start, DateTime end, ErrorType errorType) { var logRepository = new LogRepository(); var urlRepository = new UrlRepository(); var items = logRepository.List(start, end, ErrorType.ERR); List <Log> logItems = new List <Log>(); foreach (var logItem in items) { var url = urlRepository.GetByName(logItem.Url); if (url != null) { if (url.MonitorInactivity == true) { logItems.Add(logItem); } } else { logItems.Add(logItem); } } return(logItems); }
public void Log(ErrorType errorType, string message, string userName, string detail, string url, DateTime?startDate = null) { var _logRepository = new LogRepository(); var _userRepository = new UserRepository(); var _urlRepository = new UrlRepository(); var _user = _userRepository.GetByUsername(userName); if (errorType == ErrorType.INFO) { var _url = _urlRepository.GetByName(url); _url.Hits += 1; _urlRepository.Update(_url); } detail = "Username: "******": " + detail; DateTime start = startDate ?? DateTime.Now; var duration = (DateTime.Now - start).TotalMilliseconds; var log = new Log(DateTime.Now, (int)errorType, message, Enum.GetName(typeof(ErrorType), (int)errorType), url, detail, false, _user.Id, duration); _logRepository.Insert(log); }