Пример #1
0
        public async Task <IHttpActionResult> GetToSendBulkMail([FromUri] string passcode)
        {
            SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            if (passcode != smtpSection.Network.Password)
            {
                return(BadRequest());
            }
            object retresult = await datacontext.SelectAllPendingPrimaryEvidence();

            if (retresult.GetType().ToString() != "System.String")
            {
                List <Personnel_with_pending_primary_evidence> list = (List <Personnel_with_pending_primary_evidence>)retresult;
                foreach (Personnel_with_pending_primary_evidence item in list)
                {
                    //send mail!
                    object sendresult = await MailingUtils.sendNotificationAllPendingPrimaryEvidence(item);

                    while (sendresult != null)
                    {
                        //send again if fail
                        sendresult = await MailingUtils.sendNotificationAllPendingPrimaryEvidence(item);
                    }
                }
                return(Ok());
            }
            else
            {
                return(InternalServerError(new Exception(retresult.ToString())));
            }
        }
Пример #2
0
        public async Task <IHttpActionResult> PostForCreateNewAdmin(JObject data)
        {
            const string             chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            RNGCryptoServiceProvider gen   = new RNGCryptoServiceProvider();

            byte[] num = new byte[8];

            //GENERATE PASSWORD 8 CHARS
            string password = "";

            gen.GetBytes(num);
            for (int j = 0; j < 8; j++)
            {
                num[j]   %= (byte)chars.Length;
                password += chars[num[j]];
            }
            //=========================
            string emaillower = data["email"].ToString();

            emaillower = emaillower.ToLower();
            UsernamePassword u = new UsernamePassword(emaillower, password);

            datacontext.admin_creator_id = Convert.ToInt32(data["user_id"]);
            datacontext.username         = emaillower;
            datacontext.password         = u.password;
            datacontext.t_name           = data["t_name"].ToString();

            object result = await datacontext.InsertWithSelect();

            if (result.GetType().ToString() != "System.String")
            {
                object sendresult = await MailingUtils.sendUsernamePasswordMailForAdmin(emaillower, password, datacontext.t_name);

                if (sendresult == null)
                {
                    return(Ok(result));
                }
                else
                {
                    return(InternalServerError((Exception)sendresult));
                }
            }
            else
            {
                return(BadRequest(result.ToString()));
            }
        }
Пример #3
0
        public async Task <IHttpActionResult> PostToSendNotifications(oPrimary_evidence data)
        {
            object result = await data.SelectPrimaryEvidenceWithTeacherDetail();

            if (result.GetType().ToString() != "System.String")
            {
                //send mail!
                object sendresult = await MailingUtils.sendNotificationPrimaryEvidenceIndividual((Evidence_with_teacher_curri_indicator_detail)result);

                if (sendresult == null)
                {
                    return(Ok());
                }
                else
                {
                    return(InternalServerError((Exception)sendresult));
                }
            }
            else
            {
                return(InternalServerError(new Exception(result.ToString())));
            }
        }
Пример #4
0
        public async Task <IHttpActionResult> PostForCreateNewUsersByFile()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.UnsupportedMediaType, Request));
            }

            string savepath = WebApiApplication.SERVERPATH + "temp";
            var    result   = new MultipartFormDataStreamProvider(savepath);
            List <UsernamePassword> userlist           = new List <UsernamePassword>();
            List <UsernamePassword> nonencryptuserlist = new List <UsernamePassword>();
            List <string>           curri_list         = new List <string>();

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

                //1.READ JSON DATA PART (curri and type)
                JObject datareceive = JObject.Parse(result.FormData.GetValues(result.FormData.AllKeys[0])[0]);
                if (datareceive["curri"] != null)
                {
                    JArray jcurrilist = (JArray)datareceive["curri"];
                    foreach (JValue value in jcurrilist)
                    {
                        curri_list.Add(value.ToString());
                    }
                }
                int select_user_type = Convert.ToInt32(datareceive["type"]["user_type_id"]);


                //2.Read maillist file

                MultipartFileData file = result.FileData[0];
                if (file.Headers.ContentType.ToString() != "text/plain")
                {
                    return(BadRequest("ไฟล์ที่อัพโหลดไม่เป็นไฟล์ข้อความธรรมดา"));
                }

                FileInfo fileInfo = new FileInfo(file.LocalFileName);

                const string             chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                RNGCryptoServiceProvider gen   = new RNGCryptoServiceProvider();
                byte[]   num       = new byte[8];
                string   text      = File.ReadAllText(string.Format("{0}/{1}", savepath, fileInfo.Name));
                string[] emaillist = text.Split(' ', '\r', '\t', '\n', '\v', '\f').Where(t => t != "").ToArray();
                foreach (string str in emaillist)
                {
                    //GENERATE PASSWORD 8 CHARS
                    string password = "";
                    gen.GetBytes(num);
                    for (int j = 0; j < 8; j++)
                    {
                        num[j]   %= (byte)chars.Length;
                        password += chars[num[j]];
                    }
                    //=========================
                    string strlower = str.ToLower();

                    userlist.Add(new UsernamePassword(strlower, password));
                    nonencryptuserlist.Add(new UsernamePassword
                    {
                        username = strlower,
                        password = password
                    });
                }

                //3.Delete temp email file
                //Check whether file exists!
                if (File.Exists(string.Format("{0}/{1}", savepath, fileInfo.Name)))
                {
                    File.Delete(string.Format("{0}/{1}", savepath, fileInfo.Name));
                }

                //4.Add user to database base on select_user_type
                object resultfromdb = null;
                if (select_user_type == 1)
                {
                    resultfromdb = await teachercontext.Insert(userlist, curri_list);
                }
                else if (select_user_type == 2)
                {
                    resultfromdb = await staffcontext.Insert(userlist, curri_list);
                }
                else if (select_user_type == 3)
                {
                    resultfromdb = await studentcontext.Insert(userlist, curri_list);
                }
                else if (select_user_type == 4)
                {
                    resultfromdb = await alumnicontext.Insert(userlist, curri_list);
                }
                else if (select_user_type == 5)
                {
                    resultfromdb = await companycontext.Insert(userlist, curri_list);
                }
                else if (select_user_type == 6)
                {
                    resultfromdb = await assessorcontext.Insert(userlist, curri_list);
                }
                else if (select_user_type != 0)
                {
                    resultfromdb = await userscontext.InsertWithUserType(userlist, curri_list, select_user_type);
                }
                else
                {
                    return(BadRequest("กรุณาเลือกประเภทผู้ใช้งาน"));
                }

                //5.If result is list,try to send mail (send only mail that not it resultfromdb)
                //Otherwise send errorresult back to user.
                if (resultfromdb == null)
                {
                    foreach (UsernamePassword item in nonencryptuserlist)
                    {
                        //send mail!
                        await MailingUtils.sendUsernamePasswordMail(item.username, item.password);
                    }
                    return(Ok());
                }
                else if (resultfromdb.GetType().ToString() != "System.String")
                {
                    List <string> erroremail = (List <string>)resultfromdb;
                    if (userlist.Count == erroremail.Count)
                    {
                        return(BadRequest("ทุกอีเมล์ในไฟล์ดังกล่าวมีอยู่แล้วในระบบ"));
                    }
                    foreach (UsernamePassword item in nonencryptuserlist)
                    {
                        //If current mail is not in erroremail => SEND!
                        if (erroremail.FirstOrDefault(t => t == item.username) == null)
                        {
                            //send mail!
                            await MailingUtils.sendUsernamePasswordMail(item.username, item.password);
                        }
                    }
                    return(Ok(resultfromdb));
                }
                else
                {
                    return(InternalServerError(new Exception(resultfromdb.ToString())));
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
Пример #5
0
        public async Task <IHttpActionResult> PostForCreateNewUsersByTyping(JObject data)
        {
            List <UsernamePassword> userlist           = new List <UsernamePassword>();
            List <UsernamePassword> nonencryptuserlist = new List <UsernamePassword>();
            List <string>           curri_list         = new List <string>();

            //1.READ JSON DATA PART (curri and type)
            if (data["curri"] != null)
            {
                JArray jcurrilist = (JArray)data["curri"];
                foreach (JValue value in jcurrilist)
                {
                    curri_list.Add(value.ToString());
                }
            }
            int select_user_type = Convert.ToInt32(data["type"]["user_type_id"]);


            //2.Read maillist
            const string             chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            RNGCryptoServiceProvider gen   = new RNGCryptoServiceProvider();

            byte[] num       = new byte[8];
            JArray emaillist = (JArray)data["new_emails"];

            foreach (JValue value in emaillist)
            {
                //GENERATE PASSWORD 8 CHARS
                string password = "";
                gen.GetBytes(num);
                for (int j = 0; j < 8; j++)
                {
                    num[j]   %= (byte)chars.Length;
                    password += chars[num[j]];
                }
                //=========================
                string strlower = value.ToString().ToLower();

                userlist.Add(new UsernamePassword(strlower, password));
                nonencryptuserlist.Add(new UsernamePassword
                {
                    username = strlower,
                    password = password
                });
            }

            //3.Add user to database base on select_user_type
            object resultfromdb = null;

            if (select_user_type == 1)
            {
                resultfromdb = await teachercontext.Insert(userlist, curri_list);
            }
            else if (select_user_type == 2)
            {
                resultfromdb = await staffcontext.Insert(userlist, curri_list);
            }
            else if (select_user_type == 3)
            {
                resultfromdb = await studentcontext.Insert(userlist, curri_list);
            }
            else if (select_user_type == 4)
            {
                resultfromdb = await alumnicontext.Insert(userlist, curri_list);
            }
            else if (select_user_type == 5)
            {
                resultfromdb = await companycontext.Insert(userlist, curri_list);
            }
            else if (select_user_type == 6)
            {
                resultfromdb = await assessorcontext.Insert(userlist, curri_list);
            }
            else if (select_user_type != 0)
            {
                resultfromdb = await userscontext.InsertWithUserType(userlist, curri_list, select_user_type);
            }
            else
            {
                return(BadRequest("กรุณาเลือกประเภทผู้ใช้งาน"));
            }

            //4.If result is list,try to send mail (send only mail that not it resultfromdb)
            //Otherwise send errorresult back to user.
            if (resultfromdb == null)
            {
                foreach (UsernamePassword item in nonencryptuserlist)
                {
                    //send mail!
                    await MailingUtils.sendUsernamePasswordMail(item.username, item.password);
                }
                return(Ok());
            }
            else if (resultfromdb.GetType().ToString() != "System.String")
            {
                List <string> erroremail = (List <string>)resultfromdb;
                if (userlist.Count == erroremail.Count)
                {
                    return(BadRequest("ทุกอีเมล์ที่ต้องการสร้างมีอยู่แล้วในระบบ"));
                }
                foreach (UsernamePassword item in nonencryptuserlist)
                {
                    //If current mail is not in erroremail => SEND!
                    if (erroremail.FirstOrDefault(t => t == item.username) == null)
                    {
                        //send mail!
                        await MailingUtils.sendUsernamePasswordMail(item.username, item.password);
                    }
                }
                return(Ok(resultfromdb));
            }
            else
            {
                return(InternalServerError(new Exception(resultfromdb.ToString())));
            }
        }