Ejemplo n.º 1
0
        public IHttpActionResult RegisterUser([FromBody] TicketCenterAPI.Models.User user)
        {
            //insert into clients and users
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                try
                {
                    context.ins_client_registration(user.FirstName, user.LastName, user.Email, user.Password);
                }
                catch
                {
                    return(InternalServerError());
                }
                //by default ticket are open

                return(Ok("User registered succesfull"));
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult GetUserRoleById(int id)
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                var user = context.sp_get_users_roles_by_id(id);

                if (user == null)
                {
                    return(NotFound());
                }

                //convert list to json
                string jsonArrayString = Newtonsoft.Json.JsonConvert.SerializeObject(user);

                JArray jsonObjects = JArray.Parse(jsonArrayString);
                return(Ok(jsonObjects));
            }
        }
Ejemplo n.º 3
0
        public IHttpActionResult UpdateTechCat(dynamic data)
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                try
                {
                    //alternative => req.Content.ReadAsStringAsync().Result;

                    //object then unbox to int
                    int userId = data.UserId;

                    if (data.CategoryId == null)
                    {
                        bool inChat = data.InChat;
                        context.usp_tech_cat(null, userId, inChat);
                    }

                    else
                    {
                        int categoryId = data.CategoryId;
                        context.usp_tech_cat(categoryId, userId, null);
                    }

                    //   System.Diagnostics.Debug.WriteLine(roleId + " and " + categoryId);
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    return(NotFound());
                }

                return(Ok("Update succesfull"));
            }
        }
Ejemplo n.º 4
0
        public IHttpActionResult GetAllTechs()
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                var techs = context.sp_get_all_technician();


                if (techs == null)
                {
                    return(NotFound());
                }

                //convert list to json
                string jsonArrayString = Newtonsoft.Json.JsonConvert.SerializeObject(techs);

                JArray jsonArray = JArray.Parse(jsonArrayString);

                return(Ok(jsonArray));
            }
        }
Ejemplo n.º 5
0
        public IHttpActionResult GetAllUser()
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                //get all tickets
                var users = context.sp_select_all_users();

                if (users == null)
                {
                    //convert list to json
                    return(NotFound());
                }

                string jsonArrayString = Newtonsoft.Json.JsonConvert.SerializeObject(users);

                JArray jsonObjects = JArray.Parse(jsonArrayString);

                return(Ok(jsonObjects));
            }
        }
Ejemplo n.º 6
0
        public IHttpActionResult AddEmployee(dynamic data)
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                string firstname = data.FirstName;
                string lastname  = data.LastName;
                string email     = data.Email;
                int    roleId    = data.RoleId;

                try
                {
                    if (data.CategoryId == null)
                    {
                        context.ins_user(firstname, lastname, email, roleId, null);

                        return(InternalServerError());
                    }
                    else
                    {
                        int categoryId = data.CategoryId;
                        context.ins_user(firstname, lastname, email, roleId, categoryId);
                    }
                }

                catch
                {
                    return(InternalServerError());
                }

                return(Ok("User succesfull created"));
            }
        }
Ejemplo n.º 7
0
        public IHttpActionResult UpdateUser(dynamic data)
        {
            using (var context = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                context.Configuration.ProxyCreationEnabled = false;

                //is the model with binding is incorrect
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                try
                {
                    //alternative => req.Content.ReadAsStringAsync().Result;

                    //object then unbox to int
                    int    id        = data.id;
                    int    roleId    = data.RoleId;
                    string firstName = data.FirstName;
                    string lastName  = data.LastName;

                    //update role
                    context.usp_role_user(id, roleId, firstName, lastName);


                    System.Diagnostics.Debug.WriteLine(roleId + " and ");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    return(NotFound());
                }

                return(Ok("Update succesfull"));
            }
        }
Ejemplo n.º 8
0
        // validate the username and password
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            //     context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (var db = new TicketCenterAPI.Models.ticketcenterdbEntities1())
            {
                if (db != null)
                {
                    var usersWithRoles = db.UserHasRoles.ToList();
                    var user           = db.Users.ToList();
                    if (user != null)
                    {
                        if (!string.IsNullOrEmpty(user.Where(u => u.Email == context.UserName && u.Password == context.Password).FirstOrDefault().Email))
                        {
                            //find user fix this
                            User loginUser = user.Where(u => u.Email == context.UserName && u.Password == context.Password).FirstOrDefault();

                            //add intities
                            identity.AddClaim(new Claim("username", context.UserName));
                            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));

                            var clients = db.Clients.ToList();

                            Client client = clients.Where(x => x.UserId == loginUser.id).FirstOrDefault();

                            Employee emp = db.Employees.Where(e => e.UserId == loginUser.id).FirstOrDefault();


                            string empId;

                            if (emp == null)
                            {
                                empId = "";
                            }
                            else
                            {
                                empId = emp.Id + "";
                            }

                            string clientId;

                            //************ if user is not a client  *****************
                            if (client == null)
                            {
                                clientId = "";
                            }
                            else
                            {
                                //give me the client
                                clientId = client.Id + "";
                            }


                            string roleId;

                            //get user role
                            UserHasRole userRole = usersWithRoles.Where(u => u.UserId == loginUser.id).FirstOrDefault();

                            //********** if user is a client role id is empty
                            if (userRole == null)
                            {
                                roleId = "";
                            }
                            else
                            {
                                roleId = userRole.RoleId + "";
                            }

                            var props = new AuthenticationProperties(new Dictionary <string, string>
                            {
                                {
                                    "Email", context.UserName
                                },
                                {
                                    "aId", loginUser.id + ""
                                },

                                {
                                    "FirstName", loginUser.FirstName
                                },
                                {
                                    "LastName", loginUser.LastName
                                },
                                {
                                    "ClientId", clientId
                                },
                                {
                                    "aRoleId", roleId
                                },
                                {
                                    "aEmpId", empId
                                }
                            });

                            var ticket = new AuthenticationTicket(identity, props);
                            context.Validated(ticket);
                        }
                        else
                        {
                            context.SetError("invalid_grant", "Provided username and password is incorrect");
                            context.Rejected();
                        }
                    }
                }
                else
                {
                    context.SetError("invalid_grant", "Provided username and password is incorrect");
                    context.Rejected();
                }
                return;
            }
        }