Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Deber03-UserManagement-Nicolás Toscano

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

FACULTAD DE INGENIERÍA EN SISTEMAS

ISWD813 Aplicaciones Web Avanzadas

Deber03-UserManagement
Nombre: Nicolás Toscano
Fecha: 5 de febrero de 2024

1. DESARROLLO
− Código
UserManagement.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/SiteAdmin.Master"


AutoEventWireup="true" CodeBehind="UserManagement.aspx.cs"
Inherits="WebAppPoliMusicV2.Admin.UserManagement" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
runat="server">
<div>
<h2>User Management</h2>
</div>
<div>
<asp:GridView ID="gridViewUser" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID_USER"
OnRowDataBound="gridViewUser_RowDataBound"
OnDataBound="gridViewUser_OnDataBound"
OnRowCancelingEdit="gridViewUser_RowCancelingEdit"
OnRowEditing="gridViewUser_RowEditing"
OnRowUpdating="gridViewUser_RowUpdating"
OnRowCommand="gridViewUser_RowCommand"
ShowFooter="True" OnRowDeleting="gridViewUser_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="ID" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#
Bind("ID_USER") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#
Bind("ID_USER") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%#
Bind("USERNAME") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

<asp:TextBox ID="txtNewUserName" runat="server"


></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%#
Bind("USERNAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" type="email"
Text='<%# Bind("EMAIL") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewEmail" runat="server"
type="email" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#
Bind("EMAIL") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtPassword" runat="server"
TextMode="Password" Text='<%# Bind("PASSWORD") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewPassword" runat="server"
TextMode="Password"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblPassword" runat="server"
Text='*****'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birthday" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtBirthday" runat="server" Text='<%#
Eval("BIRTHDAY", "{0:dd/MM/yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewBirthday" runat="server"
></asp:TextBox>
</FooterTemplate>
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

<ItemTemplate>
<asp:Label ID="lblBirthday" runat="server" Text='<%#
Eval("BIRTHDAY", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type" HeaderStyle-
HorizontalAlign="Left">
<EditItemTemplate>
<asp:DropDownList ID="ddlType" runat="server">
<asp:ListItem Value="0">Normal</asp:ListItem>
<asp:ListItem Value="1">Administrator</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewType" runat="server">
<asp:ListItem Value="0">Normal</asp:ListItem>
<asp:ListItem Value="1">Administrator</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlType" Enabled="false"
runat="server">
<asp:ListItem>User Type</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Photo" HeaderStyle-


HorizontalAlign="Left">
<EditItemTemplate>
<asp:Image ID="imageEditUserPhoto" runat="server"
ImageUrl='<%# Eval("USER_PHOTO") %>' Width="25" Height="25" />
<asp:FileUpload ID="fileUploadUserPhoto" runat="server"
/>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="imageUserPhoto" runat="server"
ImageUrl='<%# String.Format("{0}?{1}", Eval("USER_PHOTO"),
DateTime.Now.Ticks.ToString()) %>' Width="100" Height="100" />
</ItemTemplate>
<FooterTemplate>
<asp:FileUpload ID="fileUploadNewUserPhoto"
runat="server" />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Edit" ShowHeader="False"


HeaderStyle-HorizontalAlign="Left">
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server"
CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server"
CausesValidation="False" CommandName="Insert"
Text="Insert"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server"
CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True"


ShowHeader="True" />
</Columns>
</asp:GridView>

</div>
<div>
<asp:Panel ID="frmConfirmation" Visible="true" Runat="server">
<asp:Label id="lblMessage" ForeColor="Red" Font-Bold="true"
Runat="server"></asp:Label>
</asp:Panel>
</div>

</asp:Content>

UserManagement.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPoliMusicV2.ServiceReferenceUser;
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

using WebAppPoliMusicV2.Util;

namespace WebAppPoliMusicV2.Admin
{
public partial class UserManagement : System.Web.UI.Page
{
int userId;
User user;

protected void Page_Load(object sender, EventArgs e)


{
if (!string.IsNullOrEmpty(Request.QueryString["uid"]))
userId = Convert.ToInt32(Request.QueryString["uid"]);
else
//userId = Convert.ToInt32(Session[Constant.USER]);
//userId = Convert.ToInt32(Request.QueryString["uid"]);
user = (User)Session[Constants.USER];
userId = user.Id;
if (!Page.IsPostBack)
{
BindData();
}
lblMessage.Text = string.Empty;
}
private void BindData()
{
DataSet dsUser = new
ServiceReferenceUser.WebServiceUserSoapClient().Read();
if (dsUser != null && dsUser.Tables.Count > 0)
{
DataTable dtUser = dsUser.Tables[0];
if (dtUser != null && dtUser.Rows.Count > 0)
{
gridViewUser.DataSource = dtUser;
gridViewUser.DataBind();
}
else
{
dtUser.Rows.Add(dtUser.NewRow());
gridViewUser.DataSource = dtUser;
gridViewUser.DataBind();
int columncount = gridViewUser.Rows[0].Cells.Count;
gridViewUser.Rows[0].Cells.Clear();
gridViewUser.Rows[0].Cells.Add(new TableCell());
gridViewUser.Rows[0].Cells[0].ColumnSpan = columncount;
gridViewUser.Rows[0].Cells[0].Text = "No Records Found";
}
}
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

}
protected void gridViewUser_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType
== DataControlRowType.Footer)
{
DropDownList ddlType = e.Row.FindControl(e.Row.RowState ==
DataControlRowState.Edit ? "ddlType" : "ddlNewType") as DropDownList;

if (ddlType != null)
{
// Assuming that you have a DataTable as the DataSource
for the GridView
DataRowView drv = e.Row.DataItem as DataRowView;

if (drv != null)
{
// Assuming that "USER_TYPE" is the column name in
your DataTable
if (drv.Row.Table.Columns.Contains("USER_TYPE"))
{
int userTypeValue =
Convert.ToInt32(drv["USER_TYPE"]);

// Set the selected value based on the


userTypeValue
ddlType.SelectedValue =
userTypeValue.ToString();
}
}
}
}
}

protected void gridViewUser_OnDataBound(object sender, EventArgs e)


{

}
protected void gridViewUser_RowEditing(object sender,
GridViewEditEventArgs e)
{
gridViewUser.EditIndex = e.NewEditIndex;
BindData();
}
protected void gridViewUser_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

{
gridViewUser.EditIndex = -1;
BindData();
}

protected void gridViewUser_RowCommand(object sender,


GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{

// Access the controls in the footer template for inserting


TextBox txtNewUserName =
(TextBox)gridViewUser.FooterRow.FindControl("txtNewUserName");
TextBox txtNewEmail =
(TextBox)gridViewUser.FooterRow.FindControl("txtNewEmail");
TextBox txtNewPassword =
(TextBox)gridViewUser.FooterRow.FindControl("txtNewPassword");
TextBox txtNewBirthday =
(TextBox)gridViewUser.FooterRow.FindControl("txtNewBirthday");
DropDownList ddlNewType =
(DropDownList)gridViewUser.FooterRow.FindControl("ddlNewType");
FileUpload fileUploadNewUserPhoto =
(FileUpload)gridViewUser.FooterRow.FindControl("fileUploadNewUserPhoto");

string messageValidations = string.Empty;


string userName = txtNewUserName.Text;
string password = txtNewPassword.Text;
string email = txtNewEmail.Text;
string birthday = string.Empty;
if (txtNewBirthday.Text != string.Empty)
{
//birthday = DateTime.ParseExact(txtBirthDate.Text,
"yyyy-MM-dd", null).ToString();
DateTime d;
if (DateTime.TryParseExact(txtNewBirthday.Text,
"dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out d))
{
birthday = d.ToString("yyyy-MM-dd");
}
}
string photoName = fileUploadNewUserPhoto.FileName;
int type = Convert.ToInt32(ddlNewType.SelectedValue);

string newImageName = userName.Replace(" ", string.Empty);


FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

string strImageFolder =
ConfigurationManager.AppSettings["userPhotoPath"];
string strFileNameExtension =
Path.GetExtension(fileUploadNewUserPhoto.FileName);
string newFileNameWithExtension = strImageFolder +
newImageName + strFileNameExtension;

User user = new User();


user.Name = userName;
user.Email = email;
user.Password = password;
user.Birthday = Convert.ToDateTime(birthday);
user.Type = type;
user.Photo = newFileNameWithExtension;

messageValidations = UserFieldsRequiredValidations(user);
if (messageValidations != string.Empty)
{
lblMessage.Text = messageValidations;
return;
}
messageValidations = new
ServiceReferenceUser.WebServiceUserSoapClient().ValidationsDuplicated(user);
if (messageValidations != string.Empty)
{
lblMessage.Text = messageValidations;
return;
}
if (fileUploadNewUserPhoto.HasFile)
{
messageValidations = new
FileManagement().SaveImageOnServer(fileUploadNewUserPhoto, strImageFolder,
newImageName);
}
else
{
messageValidations = new
FileManagement().SaveDefaultImageOnServer(strImageFolder, newImageName);
user.Photo += ".jpg";
}
if (messageValidations != string.Empty)
{
lblMessage.Text = messageValidations;
return;
}
user.Password = Util.Hash.GeneratePasswordHash(password);
new
ServiceReferenceUser.WebServiceUserSoapClient().AddUser(user);
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

lblMessage.Text = string.Empty;

BindData();
gridViewUser.SelectedIndex = -1;
}
}

public string UserFieldsRequiredValidations(User user)


{
if (user.Name == string.Empty)
{
return "Username is required!";
}
if (user.Email == string.Empty)
{
return "Email is required!";
}
if (user.Password == string.Empty)
{
return "Password is required!";
}
if (!Util.Validator.IsValidEmail(user.Email))
{
return "Email is not valid!";
}
return string.Empty;
}

protected void gridViewUser_RowUpdating(object sender,


GridViewUpdateEventArgs e)
{
string messageValidations = string.Empty;
Label lblId = gridViewUser.Rows[e.RowIndex].FindControl("lblId")
as Label;
TextBox txtUserName =
(TextBox)gridViewUser.Rows[e.RowIndex].FindControl("txtUserName");
TextBox txtEmail =
(TextBox)gridViewUser.Rows[e.RowIndex].FindControl("txtEmail");
TextBox txtPassword =
(TextBox)gridViewUser.Rows[e.RowIndex].FindControl("txtPassword");
TextBox txtBirthday =
(TextBox)gridViewUser.Rows[e.RowIndex].FindControl("txtBirthday");
DropDownList ddlType =
(DropDownList)gridViewUser.Rows[e.RowIndex].FindControl("ddlType");
FileUpload fileUploadUserPhoto =
(FileUpload)gridViewUser.Rows[e.RowIndex].FindControl("fileUploadUserPhoto")
;
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

int userId = Convert.ToInt32(lblId.Text);


string userName = txtUserName.Text;
string userEmail = txtEmail.Text;
string userPassword = txtPassword.Text;
string userBirthday = txtBirthday.Text;
int userType = Convert.ToInt32(ddlType.SelectedValue);

string title = txtUserName.Text;

string newImageName = title.Replace(" ", string.Empty);

System.Web.UI.WebControls.Image imageEditPlaylistPhoto =
(System.Web.UI.WebControls.Image)gridViewUser.Rows[e.RowIndex].FindControl("
imageEditUserPhoto");
string strImageFolder =
ConfigurationManager.AppSettings["playlistFolderPath"];
string strFileNameExtension =
Path.GetExtension(fileUploadUserPhoto.FileName);
string newFileNameWithExtension = strImageFolder + newImageName
+ strFileNameExtension;

User user = new User();


user.Id = userId;
user.Name = userName;
user.Email = userEmail;
user.Password = userPassword;
user.Birthday = Convert.ToDateTime(userBirthday);
user.Type = userType;

if (fileUploadUserPhoto.HasFile)
{
user.Photo = newFileNameWithExtension;
}
else
{
user.Photo = imageEditPlaylistPhoto.ImageUrl;
}

messageValidations = UserValidations(user);
if (messageValidations != string.Empty)
{
lblMessage.Text = messageValidations;
return;
}
int returnValue = new
ServiceReferenceUser.WebServiceUserSoapClient().CheckExistUser(user.Name);
if (returnValue != 0)
{
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

lblMessage.Text = "User already registered!";


return;
}

new
ServiceReferenceUser.WebServiceUserSoapClient().UpdateUser(user.Id,
user.Name, user.Password, user.Email, user.Birthday, user.Photo ,user.Type);
lblMessage.Text = string.Empty;
gridViewUser.EditIndex = -1;
BindData();
}

protected void gridViewUser_RowDeleting(object sender,


GridViewDeleteEventArgs e)
{
int id =
Convert.ToInt32(gridViewUser.DataKeys[e.RowIndex].Values[0]);
new
ServiceReferenceUser.WebServiceUserSoapClient().DeleteUser(id);
BindData();
}

protected void gridViewUser_SelectedIndexChanged(object sender,


EventArgs e)
{

public string UserValidations(ServiceReferenceUser.User user)


{
if (user.Name == string.Empty)
{
return "User Name is required!"; ;
}
int returnValue = new
ServiceReferenceUser.WebServiceUserSoapClient().CheckExistUser(user.Name);
if (returnValue != 0)
{
return "User already exist!";
}
return string.Empty;
}
}

}
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

2. Operaciones CRUD
− READ
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

− CREATE

− UPDATE
FACULTAD DE INGENIERÍA EN SISTEMAS
ISWD813 Aplicaciones Web Avanzadas

− DELETE

You might also like