示例#1
0
 public static bool AddStaff(Staff staff)
 {
     if (staff == null)
     {
         return false;
     }
     try
     {
         db.Staffs.Add(staff);
         db.SaveChanges();
     }
     catch (Exception e)
     {
         string s = e.ToString();
         return false;
     }
     return true;
 }
示例#2
0
        public int AddStaff(FormCollection f)
        {
            try
            {
                AccountBusiness ab = new AccountBusiness();
                User staffUser = Session["User"] as User;
                if (staffUser == null || Session["UserRole"] == null || (int)Session["UserRole"] != 1)
                {
                    return -7;
                }
                else
                {
                    String staffName = f["txtStaffName"];
                    String staffUserAccount = f["txtStaffUserAccount"];
                    String staffPhoneNumber = f["txtStaffPhoneNumber"];
                    String staffEmail = f["txtStaffEmail"];
                    String staffAddress = f["txtStaffAddress"];
                    Staff staff = new Staff();
                    User user = new User();

                    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                    var stringChars = new char[6];
                    var random = new Random();
                    for (int i = 0; i < stringChars.Length; i++)
                    {
                        stringChars[i] = chars[random.Next(chars.Length)];
                    }
                    var finalString = new String(stringChars);

                    try
                    {
                        user.Fullname = staffName;
                        user.Username = staffUserAccount;
                        user.Email = staffEmail;
                        user.RoleId = 3;
                        user.Password = ab.CreateStaffPassword(finalString);
                        staff.StaffPhoneNumber = staffPhoneNumber;
                        staff.StaffAddress = staffAddress;
                        staff.IsActive = true;

                        staff.User = user;

                        string password = "******";
                        string from = "*****@*****.**";
                        string to = staffEmail;

                        MailMessage mail = new MailMessage();
                        mail.IsBodyHtml = true;
                        mail.To.Add(to);
                        mail.From = new MailAddress(from);
                        mail.Subject = string.Format("{0}{1}", "Tạo tài khoản cho nhân viên ", staffName);
                        mail.Body += "<html lang='vi'>";
                        mail.Body += "<head>";
                        mail.Body += "<meta charset='utf-8'>";
                        mail.Body += "</head>";
                        mail.Body += "<body>";
                        mail.Body += "<div> Bạn vừa được tạo tài khoản tại Tiệm Bánh Dâu Tây</div>";
                        mail.Body += string.Format("{0}{1}", "Tên tài khoản: ", staffUserAccount);
                        mail.Body += "<div></div>";
                        mail.Body += string.Format("{0}{1}", "Mật khẩu: ", finalString);
                        mail.Body += "</body>";
                        mail.Body += "</html>";
                        var mailBody = mail.Body;
                        var htmlBody = AlternateView.CreateAlternateViewFromString(mailBody, null, "text/html");
                        mail.AlternateViews.Add(htmlBody);

                        mail.Priority = MailPriority.High;
                        SmtpClient smtp = new SmtpClient();
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new System.Net.NetworkCredential(from, password);
                        smtp.Port = 587;
                        smtp.Host = "smtp.gmail.com";
                        smtp.EnableSsl = true;
                        smtp.Send(mail);

                    }
                    catch (Exception)
                    {
                        return 0;

                    }

                    bool result = StaffBusiness.AddStaff(staff);
                    if (result)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }