예제 #1
0
        public async Task <IHttpActionResult> PostForLogin(JObject usrpwdata)
        {
            List <System.Net.Http.Headers.CookieHeaderValue> x = Request.Headers.GetCookies("mymy").ToList();

            if (x.Count == 1)
            {
                //If login cookie exists:Return error to indicate that user already logged in
                return(BadRequest("ท่านได้เข้าสู่ระบบอยู่แล้ว"));
            }
            UsernamePassword data = new UsernamePassword();

            data.username = usrpwdata["username"].ToString();
            data.password = usrpwdata["password"].ToString();

            if (data.username == "" && data.password == "")
            {
                return(BadRequest("กรุณาใส่ชื่อผู้ใช้และรหัสผ่านที่ต้องการเข้าสู่ระบบ"));
            }
            else if (data.username == "")
            {
                return(BadRequest("กรุณาใส่ชื่อผู้ใช้งานที่ต้องการเข้าสู่ระบบ"));
            }
            else if (data.password == "")
            {
                return(BadRequest("กรุณาใส่รหัสผ่านที่ใช้ในการเข้าสู่ระบบ"));
            }
            oUsers context = new oUsers();

            data.username = data.username.ToLower();
            object result = await context.SelectUser(data.username);

            //Check whether login is success?
            if (result.GetType().ToString() != "System.String")
            {
                User_information_with_privilege_information u = (User_information_with_privilege_information)result;
                string oldpassword = data.password;
                data.password = u.information.GetPassword();
                if (data.isMatchPassword(oldpassword))
                {
                    return(Ok(u));
                }
                else
                {
                    return(BadRequest("ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง"));
                }
            }
            else
            {
                return(BadRequest("ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง"));
            }
        }
예제 #2
0
        public async Task <IHttpActionResult> PostForQueryUserData([FromBody] int user_id)
        {
            oUsers datacontext = new oUsers();
            object result      = await datacontext.selectUserData(user_id);

            if (result.GetType().ToString() != "System.String")
            {
                return(Ok(result));
            }
            else
            {
                return(InternalServerError(new Exception(result.ToString())));
            }
        }
예제 #3
0
        public async Task <IHttpActionResult> PostForResetPassword([FromBody] int user_id)
        {
            oUsers datacontext = new oUsers();

            object result = await datacontext.ResetPassword(user_id);

            if (result == null)
            {
                return(Ok());
            }
            else
            {
                return(InternalServerError(new Exception(result.ToString())));
            }
        }
예제 #4
0
        public async Task <IHttpActionResult> PutForChangeUsername(JObject userdata)
        {
            oUsers datacontext = new oUsers();
            string username    = userdata["username"].ToString().ToLower();
            int    user_id     = Convert.ToInt32(userdata["user_id"]);

            object result = await datacontext.UpdateUsername(username, user_id);

            if (result == null)
            {
                return(Ok());
            }
            else
            {
                return(InternalServerError(new Exception(result.ToString())));
            }
        }
예제 #5
0
        public IHttpActionResult PutForChangePassword(JObject userdata)
        {
            oUsers datacontext  = new oUsers();
            string old_password = userdata["old_password"].ToString();
            string new_password = userdata["new_password"].ToString();
            int    user_id      = Convert.ToInt32(userdata["user_id"]);

            object result = datacontext.UpdatePassword(old_password, ref new_password, user_id);

            if (result == null)
            {
                return(Ok());
            }
            else
            {
                return(InternalServerError(new Exception(result.ToString())));
            }
        }
예제 #6
0
        public async Task <IHttpActionResult> PutForUpdateUserData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.UnsupportedMediaType, Request));
            }

            string savepath = WebApiApplication.SERVERPATH + "myImages/profile_pic";
            var    result   = new MultipartFormDataStreamProvider(savepath);

            try
            {
                await Request.Content.ReadAsMultipartAsync(result);

                oUsers datacontext = new oUsers();
                //READ JSON DATA PART
                JObject datareceive = JObject.Parse(result.FormData.GetValues(result.FormData.AllKeys[0])[0]);
                User_information_with_privilege_information userdata = new User_information_with_privilege_information();



                //Prerequisite
                userdata.user_id = Convert.ToInt32(datareceive["user_id"]);
                //username ignored
                //citizen_id ignored
                //gender ignored
                //timestamp ignored

                //teacher section => degree ignored
                //teacher section => position ignored
                //teacher section => personnel_type ignored
                //teacher section => person_id ignored
                //teacher,staff section => room ignored
                //teacher section => alive ignored

                userdata.user_type = datareceive["user_type"].ToString();

                //list of update value
                userdata.information.t_prename = datareceive["information"]["t_prename"].ToString();
                userdata.information.t_name    = datareceive["information"]["t_name"].ToString();
                userdata.information.e_prename = datareceive["information"]["e_prename"].ToString();
                userdata.information.e_name    = datareceive["information"]["e_name"].ToString();
                userdata.information.email     = datareceive["information"]["email"].ToString();
                userdata.information.tel       = datareceive["information"]["tel"].ToString();
                userdata.information.addr      = datareceive["information"]["addr"].ToString();


                if (userdata.user_type == "อาจารย์")
                {
                    //teacher have status
                    userdata.information.status = datareceive["information"]["status"].ToString();
                    //teacher have interest
                    if (datareceive["information"]["interest"] != null)
                    {
                        JArray interestarr = (JArray)datareceive["information"]["interest"];
                        foreach (JValue value in interestarr)
                        {
                            userdata.information.interest.Add(value.ToString());
                        }
                    }
                }

                if (userdata.user_type != "นักศึกษา")
                {
                    if (datareceive["information"]["education"] != null)
                    {
                        JArray educationarr = (JArray)datareceive["information"]["education"];
                        foreach (JObject eduitem in educationarr)
                        {
                            userdata.information.education.Add(new Models.Educational_teacher_staff {
                                education_id = Convert.ToInt32(eduitem["education_id"])
                            });
                        }
                    }
                }
                //filenamepic will add later

                if (result.FileData.Count > 0)
                {
                    MultipartFileData file     = result.FileData[0];
                    FileInfo          fileInfo = new FileInfo(file.LocalFileName);
                    if (!file.Headers.ContentType.ToString().Contains("image/"))
                    {
                        //Delete temp upload file
                        if (File.Exists(string.Format("{0}/{1}", savepath, fileInfo.Name)))
                        {
                            File.Delete(string.Format("{0}/{1}", savepath, fileInfo.Name));
                        }
                        return(BadRequest("ไฟล์รูปภาพที่ท่านอัพโหลดไมใช่ไฟล์รูปภาพที่ถูกต้อง"));
                    }
                    string newfilename = string.Format("{0}.{1}", fileInfo.Name.Substring(9), file.Headers.ContentDisposition.FileName.Split('.').LastOrDefault().Split('\"').FirstOrDefault());
                    userdata.information.file_name_pic = "myImages/profile_pic/" + newfilename;
                    File.Move(string.Format("{0}/{1}", savepath, fileInfo.Name), string.Format("{0}/{1}", savepath, newfilename));
                }
                else
                {
                    //file_name_pic set to null => no change!
                    userdata.information.file_name_pic = null;
                }

                object resultfromdb = await datacontext.UpdateUserData(userdata);

                if (resultfromdb.GetType().ToString() != "System.String")
                {
                    //delete filename will inside file_name property of oUser object
                    string delpath = WebApiApplication.SERVERPATH;
                    if (datacontext.file_name_pic != null)
                    {
                        //Check whether file exists!
                        if (File.Exists(string.Format("{0}{1}", delpath, datacontext.file_name_pic)))
                        {
                            File.Delete(string.Format("{0}{1}", delpath, datacontext.file_name_pic));
                        }
                    }
                    return(Ok(resultfromdb));
                }
                else
                {
                    return(InternalServerError(new Exception(resultfromdb.ToString())));
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }