public bool Update(UsersExt model, ModelStateDictionary modelState, Controller ctrl)
        {
            bool status = true;
            //Wrap it all in a transaction
            TransactionOptions transOptions = SetTransactionTimeoutForDebugging(HttpContext.Current);

            using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, transOptions))
            {
                //if (db.tblUsers.Any(u => u.UserID != model.UserID && u.Username.ToLower().Equals(model.Username)))
                //{
                //    status = false;
                //    modelState.AddModelError("Username", "Username already Exists.");
                //}
                //else
                //{
                //    //TODO: Map to DB Object
                //    tblUsers tbluser = Map(model);

                //    tbluser.Password = SecurityUtils.EncryptText(tbluser.Password);

                //    db.tblUsers.Attach(tbluser);
                //    db.Entry(tbluser).State = System.Data.Entity.EntityState.Modified;
                //    db.SaveChanges();

                //    //TOD: Add to Audit Log
                //    SecurityUtils.AddAuditLog("User Details has been Updated. User FullName = " + model.Fullname, ctrl);

                //    //To get here, everything must be OK, so commit the transaction
                //    transaction.Complete();
                //}
            }

            return status;
        }
        public bool Create(UsersExt model, ModelStateDictionary modelState, Controller ctrl)
        {
            bool status = true;
            //Wrap it all in a transaction

            TransactionOptions transOptions = SetTransactionTimeoutForDebugging(HttpContext.Current);

            using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, transOptions))
            {
                //if (db.tblUsers.Any(u => u.Username.ToLower().Equals(model.Username)))
                //{
                //    status = false;
                //    modelState.AddModelError("Username", "Username already Exists.");

                //}
                //else
                //{

                //    tblUsers tbluser = Map(model);

                //    tbluser.Password = SecurityUtils.EncryptText(tbluser.Password);

                //    db.tblUsers.Add(tbluser);
                //    db.SaveChanges();

                //    // UserID = tbluser.UserID;
                //    //Add to Audit Log
                //    SecurityUtils.AddAuditLog("User has been Added. User FullName = " + model.Fullname, ctrl);
                //    transaction.Complete();
                //}
            }
            return status;
        }
        public ActionResult _Destroy([DataSourceRequest] DataSourceRequest request, UsersExt model)
        {
            string Msg = "";

            UsersRepository modelRepo = new UsersRepository();

            if (modelRepo.Delete(model, ref Msg, this) == false)
            {
                return(this.Json(new DataSourceResult {
                    Errors = Msg
                }));
            }

            return(Json(request));
        }
        public ActionResult _Update([DataSourceRequest] DataSourceRequest request, UsersExt model)
        {
            if (ModelState.IsValid)
            {
                string Msg = "";

                UsersRepository modelRepo = new UsersRepository();
                if (modelRepo.Update(model, ref Msg, this) == false)
                {
                    return(this.Json(new DataSourceResult {
                        Errors = Msg
                    }));
                }
            }
            else
            {
                string error = ErrorHandling.HandleModelStateErrors(ModelState);
                return(this.Json(new DataSourceResult {
                    Errors = error
                }));
            }
            return(Json(request));
        }
        public bool Delete(UsersExt model, Controller ctrl)
        {
            bool status = true;
            //Wrap it all in a transaction
            TransactionOptions transOptions = SetTransactionTimeoutForDebugging(HttpContext.Current);

            using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, transOptions))
            {
                //TODO: Get Current Object from DB
                //tblUsers tbluser = Map(model);

                ////TODO: Check if it is not null, then Remove from DB
                //db.tblUsers.Remove(tbluser);
                //db.SaveChanges();

                ////Add To Log
                //SecurityUtils.AddAuditLog("User has been Deleted FullName = " + model.Fullname, ctrl);

                ////To get here, everything must be OK, so commit the transaction
                //transaction.Complete();
            }

            return status;
        }
 //public tblUsers Map(UsersExt model)
 //{
 //    tblUsers tbluser = new tblUsers()
 //    {
 //        UserID = model.UserID,
 //        Fullname = model.Fullname,
 //        Username = model.Username,
 //        Password = model.Password,
 //        EmailAddress = model.EmailAddress,
 //        RoleID = model.RoleID,
 //        Locked = model.Locked
 //    };
 //    if (model.RowVersion_Str != null)
 //    {
 //        tbluser.RowVersion = Convert.FromBase64String(model.RowVersion_Str);
 //    }
 //    return tbluser;
 //}
 //public UsersExt Map(tblUsers model)
 //{
 //    UsersExt tbluser = new UsersExt()
 //    {
 //        UserID = model.UserID,
 //        Fullname = model.Fullname,
 //        Username = model.Username,
 //        Password = model.Password,
 //        EmailAddress = model.EmailAddress,
 //        RoleID = model.RoleID,
 //        Locked = model.Locked,
 //        RowVersion_Byte = model.RowVersion
 //    };
 //    tbluser.RowVersion_Str = Convert.ToBase64String(tbluser.RowVersion_Byte);
 //    return tbluser;
 //}
 /// <summary>
 /// Add the Action to Audit Log
 /// </summary>
 /// <param name="model">The Object for which this Auditlog took place</param>
 /// <param name="Action">"Added New User OR Updated User Details OR Deleted User"</param>
 private void AuditLog(UsersExt model, string Action, Controller ctrl)
 {
     SecurityUtils.AddAuditLog(Action +
                         " Username = \"" + model.Username + "\"", null, db, ctrl);
 }
        public void Insert(int Id,string Name,string MobilePhone,string Email,string OfficePhone,string Username,string Position,string Status,bool? Synched)
        {
            UsersExt item = new UsersExt();

            item.Id = Id;

            item.Name = Name;

            item.MobilePhone = MobilePhone;

            item.Email = Email;

            item.OfficePhone = OfficePhone;

            item.Username = Username;

            item.Position = Position;

            item.Status = Status;

            item.Synched = Synched;

            item.Save(UserName);
        }
Exemple #8
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //Default Authentication Method

            bool validate = false;
            bool locked   = false;

            if (httpContext.User.Identity.IsAuthenticated)
            {
                UsersExt LoggedInUser = SecurityUtils.GetUserDetails(httpContext);

                if (LoggedInUser != null)
                {
                    if (LoggedInUser.Locked != true)
                    {
                        foreach (var item in this._permissions)
                        {
                            var _enum = Enum.Parse(typeof(Permissions), item);

                            if (Convert.ToInt32(Permissions.AllUsers) == Convert.ToInt32(_enum))
                            {
                                validate = true;
                            }

                            if (LoggedInUser.RoleID == Convert.ToInt32(_enum))
                            {
                                validate = true;
                            }
                        }
                    }
                    else
                    {
                        locked = true;
                    }
                }
            }
            else
            {
                validate = false;
            }

            if (validate)
            {
                return(true);
            }
            else
            {
                if (httpContext.Request.Url.Segments.Count() <= 1 || httpContext.Request.Url.PathAndQuery.Contains("Home"))
                {
                    httpContext.Response.StatusCode = 200;
                    httpContext.Response.Redirect("/Account/LogOn");
                    ErrorHandling.SetErrorCode("UnauthorizedAccess");
                    //The Following Code will be used to Redirect the User to their own Login Screen Based on their Role

                    //if (httpContext.Session["RoleID"] != null)
                    //{
                    //    if ((Int32)httpContext.Session["RoleID"] == (Int32)Permissions.Admin ||
                    //        (Int32)httpContext.Session["RoleID"] == (Int32)Permissions.HeadOffice ||
                    //        (Int32)httpContext.Session["RoleID"] == (Int32)Permissions.RegionalOffice)
                    //    {
                    //        httpContext.Response.Redirect("/Account/LogOn");
                    //    }
                    //    else if ((Int32)httpContext.Session["RoleID"] == (Int32)Permissions.ReadOnly)
                    //    {
                    //        httpContext.Response.Redirect("/Home/LogOn");
                    //    }
                    //}
                    //else
                    //{ httpContext.Response.Redirect("/Account/LogOn"); }
                }
                else
                {
                    httpContext.Response.StatusCode = 200;
                    if (locked)
                    {
                        httpContext.Response.Redirect("/Error/UnAuthorizedAccess?Locked=True");
                    }
                    else
                    {
                        httpContext.Response.Redirect("/Error/UnAuthorizedAccess");
                    }

                    ErrorHandling.SetErrorCode("UnauthorizedAccess");
                }

                return(false);
            }
        }