protected virtual void OnProcessStarting() { LoggerBundle.Inform($"A new process of plugin '{Name}' is starting"); }
protected override void OnInitialize() { LoggerBundle.Debug($"Initializing plugin '{Name}'..."); RegisterAction("add", AddUser); }
protected Boolean IsAuthorized(out IActionResult statusCode, Func <User, Boolean> customUserAuthorization = null) { try { JwtPayload payload = _contextAuthenticatorPipe.Process(HttpContext); using (DataContext context = DataContextFactory.GetInstance()) { User user = context.SetUsers.Include(x => x.Invites) .ThenInclude(x => x.CreateUser) .Include(x => x.Invite) .ThenInclude(x => x.RegisteredUser) .Include(x => x.Playlists) .ThenInclude(x => x.PlaylistEntries) .Include(x => x.Playlists) .ThenInclude(x => x.CreateUser) .Include(x => x.Playlists) .ThenInclude(x => x.PlaylistPermissions) .Include(x => x.PlaylistEntries) .ThenInclude(x => x.CreateUser) .Include(x => x.PlaylistEntries) .ThenInclude(x => x.Playlist) .Include(x => x.PlaylistEntries) .ThenInclude(x => x.Track) .Include(x => x.PlaylistPermissions) .ThenInclude(x => x.Playlist) .Include(x => x.PlaylistPermissions) .ThenInclude(x => x.User) .FirstOrDefault(x => x.UniqueId.Equals(payload.ClientId) && x.Username.ToLower().Equals(payload.Name)); if (null == user) { LoggerBundle.Warn($"Got valid payload for user which is not in database: '{payload.Name}'"); statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized); return(false); } if (null != customUserAuthorization) { Boolean customAuthorization = customUserAuthorization.Invoke(user); if (!customAuthorization) { LoggerBundle.Warn($"Got valid payload for user '{payload.Name}' but custom authorization failed"); statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized); return(false); } } AuthorizedUser = user; } statusCode = null; AuthorizedPayload = payload; return(true); } catch (Exception ex) { LoggerBundle.Trace(ex); statusCode = StatusCode((Int32)HttpStatusCode.Unauthorized); return(false); } }
private void AddUser() { LoggerBundle.Debug("Starting process to add new user..."); try { // read username LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a username: "******""; if (String.IsNullOrWhiteSpace(username)) { LoggerBundle.Fatal(new ArgumentException("Username cannot be empty")); Environment.Exit(1); } // check existance LoggerBundle.Debug("Checking if user already exists..."); Boolean exists; using (DataContext dataContext = DataContextFactory.GetInstance()) { exists = dataContext.SetUsers.Any(x => x.Username.ToLower().Equals(username.ToLower())); } if (exists) { LoggerBundle.Fatal(new ArgumentException("Username already exists")); Environment.Exit(1); } LoggerBundle.Trace("User not found database. Allowed to proceed forward"); // get password LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a password: "******"Confirm password: "******"Passwords do not match")); Environment.Exit(1); } // hash password Sha512HashPipe hashPipe = new Sha512HashPipe(); String hashedPw = hashPipe.Process(pw1); // save model User user = new User { Username = username , Password = hashedPw }; using (DataContext dataContext = DataContextFactory.GetInstance()) { dataContext.SetUsers.Add(user); dataContext.SaveChanges(); } LoggerBundle.Inform( $"Successfully created user '{user.Username}' created with unique identifier '{user.UniqueId}'"); } catch (Exception ex) { LoggerBundle.Error(ex); } }
protected IActionResult HandleException(Exception ex) { LoggerBundle.Error(ex); return(StatusCode((Int32)HttpStatusCode.InternalServerError)); }