Exemple #1
0
        public void Update(MariUser userParam, string password = null)
        {
            var user = _context.MariUsers.Find(userParam.ID);

            if (user == null)
            {
                throw new AppException("User not found");
            }

            // update username if it has changed
            if (!string.IsNullOrWhiteSpace(userParam.Username) && userParam.Username != user.Username)
            {
                // throw error if the new username is already taken
                if (_context.MariUsers.Any(x => x.Username == userParam.Username))
                {
                    throw new AppException("Username " + userParam.Username + " is already taken");
                }

                user.Username = userParam.Username;
            }

            // update user properties if provided
            if (!string.IsNullOrWhiteSpace(userParam.FirstName))
            {
                user.FirstName = userParam.FirstName;
            }

            if (!string.IsNullOrWhiteSpace(userParam.LastName))
            {
                user.LastName = userParam.LastName;
            }

            // update password if provided
            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;
            }

            _context.MariUsers.Update(user);
            _context.SaveChanges();
        }
Exemple #2
0
        public MariUser Create(MariUser user, string password)
        {
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         // .WriteTo.Console()
                         //.WriteTo.Console(new RenderedCompactJsonFormatter())
                         .WriteTo.File(new RenderedCompactJsonFormatter(), "../logs/log.ndjson")
                         .CreateLogger();
            Log.Information("Start User Service Controller");

            //_logger.Information("Start user service Create");
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.MariUsers.Any(x => x.Username == user.Username))
            {
                throw new AppException("Username \"" + user.Username + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;


            //_logger.Information("before EF savechanges in user service Create");
            _context.MariUsers.Add(user);
            _context.SaveChanges();



            return(user);
        }