Beispiel #1
0
        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            TextBox tbxLN         = DetailsView1.FindControl("tbxLN") as TextBox;
            TextBox tbxMB         = DetailsView1.FindControl("tbxMB") as TextBox;
            TextBox tbxPassInsert = DetailsView1.FindControl("tbxPassInsert") as TextBox;
            int     checkMB       = 0;

            Regex regPass  = new Regex(@"^.*(?=.{8,50})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!.@#$%^&+=]).*$");
            Regex regEmail = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            string Username = tbxLN.Text.ToLower() + "." + tbxMB.Text;

            using (var SCDB = new CroscoStopCardEntities())
            {
                var listUsers = SCDB.Users.Select(x => x.UserName.ToLower()).ToArray();

                if (listUsers.Contains(Username))
                {
                    GetErrorText     = "This Username: (" + Username + ") already exist!";
                    lblError.Text    = GetErrorText;
                    lblError.Visible = true;
                    e.Cancel         = true;
                }
            }

            if (string.IsNullOrWhiteSpace(((TextBox)DetailsView1.FindControl("tbxFN")).Text))
            {
                error.AppendLine("First Name is Required");
                valid = false;
            }
            if (string.IsNullOrWhiteSpace(tbxLN.Text))
            {
                error.AppendLine("Last Name is Required");
                valid = false;
            }
            if (string.IsNullOrWhiteSpace(tbxPassInsert.Text))
            {
                error.AppendLine("Password is Required");
                valid = false;
            }
            if (string.IsNullOrWhiteSpace(tbxMB.Text))
            {
                error.AppendLine("Matični Broj is Required");
                valid = false;
            }

            if (!int.TryParse(tbxMB.Text, out checkMB))
            {
                error.AppendLine("Matični Broj must be a number!");
                valid = false;
            }
            else
            {
                using (var SCDB = new CroscoStopCardEntities())
                {
                    var listMB = SCDB.Users.Select(x => x.MaticniBroj).ToArray();

                    if (listMB.Contains(checkMB))
                    {
                        GetErrorText     = "There are already user with this Matični Broj: (" + checkMB + ")!";
                        lblError.Text    = GetErrorText;
                        lblError.Visible = true;
                        e.Cancel         = true;
                    }
                }
            }

            if (!regPass.IsMatch(tbxPassInsert.Text))
            {
                error.AppendLine("Password must satisfy this conditions: minimum 8 characters long A-Z, 1 uppercase, 1 lowercase character, 1 number and 1 special character (!.@#$%^&+=)");
                valid = false;
            }

            TextBox tbxEmail = DetailsView1.FindControl("tbxMail") as TextBox;

            if (!string.IsNullOrEmpty(tbxEmail.Text))
            {
                if (!regEmail.IsMatch(tbxEmail.Text))
                {
                    error.AppendLine("Entered Email address is not valid!");
                    valid = false;
                }
            }

            if (!valid)
            {
                GetErrorText     = error.ToString();
                lblError.Text    = GetErrorText;
                lblError.Visible = true;
                e.Cancel         = true;
            }

            e.Values.Remove("Lozinka");
            e.Values.Remove("UserName");
            e.Values.Add("Lozinka", PasswordHash.CreateHash(tbxPassInsert.Text));
            e.Values.Add("UserName", Username);
            e.Values.Add("UserRole", ((DropDownList)DetailsView1.FindControl("ddlUserRole")).SelectedValue);
            e.Values.Add("OJ", ((DropDownList)DetailsView1.FindControl("ddlOJ")).SelectedValue);
        }
Beispiel #2
0
        protected void btnImportUsers_Click(object sender, EventArgs e)
        {
            if (fuExcel.FileName != string.Empty)
            {
                if (fuExcel.FileName.ToLower().EndsWith(".xls") || fuExcel.FileName.ToLower().EndsWith(".xlsx"))
                {
                    fuExcel.SaveAs(Server.MapPath("~/TempFiles/") + fuExcel.FileName);

                    string filePath = Server.MapPath("~/TempFiles/") + fuExcel.FileName;

                    IWorkbook workbook;
                    if (fuExcel.FileName.ToLower().EndsWith(".xls"))
                    {
                        using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            workbook = new HSSFWorkbook(stream);
                    }
                    else
                    {
                        using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            workbook = new XSSFWorkbook(stream);
                    }

                    ISheet    sheet = workbook.GetSheetAt(0);
                    DataTable dt    = new DataTable(sheet.SheetName);

                    IRow headerRow = sheet.GetRow(0);
                    foreach (ICell headerCell in headerRow)
                    {
                        dt.Columns.Add(headerCell.ToString());
                    }

                    int rowIndex = 0;
                    foreach (IRow row in sheet)
                    {
                        if (rowIndex++ == 0)
                        {
                            continue;
                        }
                        DataRow dataRow = dt.NewRow();
                        dataRow.ItemArray = row.Cells.Select(c => c.ToString()).ToArray();
                        dt.Rows.Add(dataRow);
                    }

                    using (var SCDB = new CroscoStopCardEntities())
                    {
                        //int lastID = SCDB.Users.OrderByDescending(u => u.UserID).FirstOrDefault().UserID;
                        var listUsers = SCDB.Users.Select(x => x.UserName).ToArray();

                        for (int i = 0; i < dt.Rows.Count - 1; i++)
                        {
                            DataRow dr = dt.Rows[i];

                            if (listUsers.Contains(dr["UserName"].ToString()))
                            {
                                continue;
                            }

                            try
                            {
                                if ((string)Session["UserRole"] == "Admin")
                                {
                                    SCDB.Users.Add(new User()
                                    {
                                        FirstName   = dr["FirstName"].ToString(),
                                        LastName    = dr["LastName"].ToString(),
                                        UserName    = dr["UserName"].ToString(),
                                        Lozinka     = PasswordHash.CreateHash(dr["Lozinka"].ToString()),
                                        MaticniBroj = Convert.ToInt32(dr["MaticniBroj"]),
                                        OJ          = (string)Session["OJ"],
                                        SubOJ       = dr["SubOJ"].ToString(),
                                        SubOJDva    = dr["SubOJDva"].ToString(),
                                        Email       = dr["Email"].ToString(),
                                        UserRole    = "User"
                                    });
                                }
                                else
                                {
                                    SCDB.Users.Add(new User()
                                    {
                                        //UserID = ++lastID,
                                        FirstName   = dr["FirstName"].ToString(),
                                        LastName    = dr["LastName"].ToString(),
                                        UserName    = dr["UserName"].ToString(),
                                        Lozinka     = PasswordHash.CreateHash(dr["Lozinka"].ToString()),
                                        MaticniBroj = Convert.ToInt32(dr["MaticniBroj"]),
                                        OJ          = dr["OJ"].ToString(),
                                        SubOJ       = dr["SubOJ"].ToString(),
                                        SubOJDva    = dr["SubOJDva"].ToString(),
                                        Email       = dr["Email"].ToString(),
                                        UserRole    = dr["UserRole"].ToString()
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                GetErrorText     = ex.Message;
                                lblError.Text    = GetErrorText;
                                lblError.Visible = true;
                            }
                        }
                        try
                        {
                            SCDB.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            GetErrorText     = ex.Message;
                            lblError.Text    = GetErrorText;
                            lblError.Visible = true;
                        }
                    }
                    File.Delete(filePath);

                    Response.Redirect("AdminPage.aspx");
                }
                else
                {
                    GetErrorText     = "Please choose an .xls or .xlsx file!";
                    lblError.Text    = GetErrorText;
                    lblError.Visible = true;
                }
            }
            else
            {
                GetErrorText     = "Please upload .xls or .xlsx file first!";
                lblError.Text    = GetErrorText;
                lblError.Visible = true;
            }
        }