public IActionResult Get()
        {
            IActionResult  ret  = null;
            List <Product> list = new List <Product>();

            try
            {
                using (var db = new PtcDbContext())
                {
                    if (db.Products.Count() > 0)
                    {
                        list = db.Products.OrderBy(p => p.ProductName).ToList();
                        ret  = StatusCode(StatusCodes.Status200OK, list);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status404NotFound, "Can't Find Products");
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex, "Exception trying to get all products");
            }

            return(ret);
        }
        public IActionResult Put([FromBody] Product entity)
        {
            IActionResult ret = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    if (entity != null)
                    {
                        db.Update(entity);
                        db.SaveChanges();
                        ret = StatusCode(StatusCodes.Status200OK, entity);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status400BadRequest, "Invalid object passed to PUT method");
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex, "Exception trying to update product: " + entity.ProductId.ToString());
            }

            return(ret);
        }
        public IActionResult Delete(int id)
        {
            IActionResult ret    = null;
            Product       entity = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    entity = db.Products.Find(id);
                    if (entity != null)
                    {
                        db.Products.Remove(entity);
                        db.SaveChanges();
                    }
                    ret = StatusCode(StatusCodes.Status200OK, true);
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex, "Exception trying to delete product: " + id.ToString());
            }

            return(ret);
        }
        public IActionResult Post([FromBody] Product entity)
        {
            IActionResult ret = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    if (entity != null)
                    {
                        db.Products.Add(entity);
                        db.SaveChanges();
                        ret = StatusCode(StatusCodes.Status201Created,
                                         entity);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status400BadRequest, "Invalid object passed to POST method");
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex, "Exception trying to insert a new product");
            }

            return(ret);
        }
Beispiel #5
0
        public IActionResult GetByName(string userName)
        {
            IActionResult ret    = null;
            AppUser       entity = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    entity = db.Users.Include(item => item.Claims).Where(u => u.UserName == userName).FirstOrDefault <AppUser>();
                    if (entity != null)
                    {
                        ret = StatusCode(StatusCodes.Status200OK, entity);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status404NotFound,
                                         "Can't Find Product: " + userName);
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex, ex.Message);
            }

            return(ret);
        }
Beispiel #6
0
        public AppUserAuth ValidateUser(AppUser user)
        {
            AppUserAuth ret      = new AppUserAuth();
            AppUser     authUser = null;

            using (var db = new PtcDbContext())
            {
                // Attempt to validate user
                var result = db.Users.Where(
                    u => u.UserName.ToLower() == user.UserName.ToLower() &&
                    u.Password == user.Password);

                if (result.Count() > 0)
                {
                    authUser = result.First();
                }
            }

            if (authUser != null)
            {
                // Build User Security Object
                ret = BuildUserAuthObject(authUser);
            }

            return(ret);
        }
        // [Authorize]
        public IActionResult Get()
        {
            IActionResult   ret  = null;
            List <Category> list = new List <Category>();

            try
            {
                using (var db = new PtcDbContext())
                {
                    if (db.Categories.Count() > 0)
                    {
                        // NOTE: Declare 'list' outside the using to avoid
                        // it being disposed before it is returned.
                        list = db.Categories.OrderBy(p => p.CategoryName).ToList();
                        ret  = StatusCode(StatusCodes.Status200OK, list);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status404NotFound,
                                         "Can't Find Categories");
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex,
                                      "Exception trying to get all Categories");
            }

            return(ret);
        }
        protected List <AppRole> GetUserRoles(AppUser authUser)
        {
            List <AppRole> list = new List <AppRole>();

            try
            {
                using (var db = new PtcDbContext())
                {
                    list.Add(new AppRole()
                    {
                        RoleId = 1, RoleName = "AdminUser", UserId = new Guid("898C9784-E31F-4F37-927F-A157EB7CA215")
                    });
                    list.Add(new AppRole()
                    {
                        RoleId = 2, RoleName = "SuperUser", UserId = new Guid("898C9784-E31F-4F37-927F-A157EB7CA215")
                    });

                    list.Add(new AppRole()
                    {
                        RoleId = 1, RoleName = "AdminUser", UserId = new Guid("4A1947EC-099C-4532-8105-64CF8C8B4B94")
                    });
                }
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Exception trying to retrieve user claims.", ex);
            }

            return(list.Where(item => item.UserId == authUser.UserId).ToList());
        }
Beispiel #9
0
        public AppUserAuth ValidateUser(AppUser user)
        {
            var     ret      = new AppUserAuth();
            AppUser authuser = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    authuser = db.Users.Where(
                        x => x.UserName.ToLower() == user.UserName.ToLower() &&
                        x.Password == user.Password
                        ).FirstOrDefault();
                }
                if (authuser != null)
                {
                    ret = BuildUserAuthObject(authuser);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception while retreving a claim", ex);
            }

            return(ret);
        }
        public IActionResult Get(int id)
        {
            IActionResult ret    = null;
            Product       entity = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    entity = db.Products.Find(id);
                    if (entity != null)
                    {
                        ret = StatusCode(StatusCodes.Status200OK, entity);
                    }
                    else
                    {
                        ret = StatusCode(StatusCodes.Status404NotFound,
                                         "Can't Find Product: " + id.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                ret = HandleException(ex,
                                      "Exception trying to retrieve a single product.");
            }

            return(ret);
        }
Beispiel #11
0
        protected List <AppUserClaim> GetUserClaims(AppUser authUser)
        {
            List <AppUserClaim> list = new List <AppUserClaim>();

            using (var db = new PtcDbContext())
            {
                list = db.Claims.Where(u => u.UserId == authUser.UserId).ToList();
            }

            return(list);
        }
Beispiel #12
0
        private List <AppUserClaim> GetUserClaims(AppUser user)
        {
            List <AppUserClaim> claims = new List <AppUserClaim> ();

            try {
                using (var db = new PtcDbContext()) {
                    claims = db.UserClaims.Where(claim => claim.UserId == user.UserId).ToList();
                }
            } catch (System.Exception ex) {
                throw ex;
            }
            return(claims);
        }
Beispiel #13
0
        protected List <AppUserClaim> GetUserClaims(AppUser authUser)
        {
            List <AppUserClaim> list = new List <AppUserClaim> ();

            try {
                using (var db = new PtcDbContext()) {
                    list = db.Claims.Where(
                        u => u.UserId == authUser.UserId).ToList();
                }
            } catch (Exception ex) {
                throw new Exception(
                          "Exception trying to retrieve user claims.", ex);
            }

            return(list);
        }
        public AppUserAuth ValidateUser(AppUser user)
        {
            AppUserAuth ret      = new AppUserAuth();
            AppUser     authUser = null;

            using (var db = new PtcDbContext())
            {
                authUser = db.AppUser.Where(u => u.UserName.ToLower() == user.UserName.ToLower() && u.Password == user.Password).FirstOrDefault();
            }

            if (authUser != null)
            {
                ret = BuildUserAuthObject(authUser);
            }

            return(ret);
        }
Beispiel #15
0
        protected List <AppUserClaim> GetUserClaimes(AppUser user)
        {
            List <AppUserClaim> list = new List <AppUserClaim>();

            try
            {
                using (var db = new PtcDbContext())
                {
                    list = db.Claims.Where(x => x.UserId == user.UserId).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Exception while retreving a claim", ex);
            }
            return(list);
        }
Beispiel #16
0
        public AppUserAuth ValidateUser(AppUser user)
        {
            AppUserAuth         userAuth = new AppUserAuth();
            List <AppUserClaim> claims   = new List <AppUserClaim> ();

            try {
                using (var db = new PtcDbContext()) {
                    var authUser = db.Users.FirstOrDefault(usr => usr.UserName.ToLower() == user.UserName && usr.Password == user.Password);
                    if (authUser != null)
                    {
                        userAuth = BuildUserAuthObject(authUser);
                    }
                }
            } catch (System.Exception ex) { // Check which one with ex or not that keeps the Call Stack
                throw ex;
            }
            return(userAuth);
        }
        public AppUserAuth ValidateUser(AppUser user)
        {
            AppUserAuth ret      = new AppUserAuth();
            AppUser     authUser = null;

            using (PtcDbContext db = new PtcDbContext())
            {
                // Attempt to validate user
                authUser = db.Users.FirstOrDefault(u =>
                                                   string.Equals(u.UserName, user.UserName, StringComparison.CurrentCultureIgnoreCase) &&
                                                   u.Password == user.Password);
            }

            if (authUser != null)
            {
                ret = BuildUserAuthObject(authUser);
            }
            return(ret);
        }
Beispiel #18
0
        public AppUserAuth AuthenticateUser(AppUser user)
        {
            AppUserAuth ret      = new AppUserAuth();
            AppUser     authUser = null;

            using (var db = new PtcDbContext())
            {
                // Attempt to validate user
                authUser = db.Users.Where(u => u.UserName.ToLower() == user.UserName.ToLower() &&
                                          u.Password == user.Password).FirstOrDefault();
            }

            if (authUser != null)
            {
                // Build User Security Object
                ret = BuildUserAuthObject(authUser);
            }

            return(ret);
        }
Beispiel #19
0
        public AppUserAuth GetNewUserClaims(AppUser user)
        {
            AppUserAuth ret      = new AppUserAuth();
            AppUser     authUser = null;

            try
            {
                using (var db = new PtcDbContext())
                {
                    if (user != null)
                    {
                        db.Users.Add(user);
                        db.SaveChanges();

                        authUser = db.Users.Where(
                            u => u.UserName.ToLower() == user.UserName.ToLower() &&
                            u.Password == user.Password).FirstOrDefault();

                        AppUserClaim userClaim = new AppUserClaim();
                        userClaim.UserId     = authUser.UserId;
                        userClaim.ClaimType  = "CanAccessMenu";
                        userClaim.ClaimValue = "true";

                        db.Claims.Add(userClaim);
                        db.SaveChanges();

                        if (authUser != null)
                        {
                            //build usersecurity object
                            ret = BuildUserAuthObject(authUser);
                        }
                    }
                }
            } catch (Exception ex) {
                throw new Exception(
                          "Exception trying to create new user.", ex);
            }
            return(ret);
        }
 public CategoryController(PtcDbContext db)
 {
     this.db = db ?? throw new ArgumentNullException(nameof(db));
 }
Beispiel #21
0
 public SecurityManager(PtcDbContext db, JwtSettings _settings)
 {
     this.db        = db ?? throw new ArgumentNullException(nameof(db));
     this._settings = _settings ?? throw new ArgumentNullException(nameof(_settings));
 }