Ejemplo n.º 1
0
        public APIResult <LoginResponse> Current()
        {
            var u = APIRequest.User(HttpContext.Current.Request);


            using (MainEntities ctx = new MainEntities())
            {
                tbl_accounts acc = ctx.tbl_accounts.FirstOrDefault(a => a.id == u.Entity.id);

                if (u == null || acc == null)
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.UserForbidden, "API_ERROR_LOGIN"));
                }

                var           AuthKey = HttpContext.Current.Request.Headers.GetValues("AUTH_KEY");
                LoginResponse l       = new LoginResponse();
                l.account = acc;
                l.token   = Guid.Parse(AuthKey.First().ToString());

                using (MainEntities dal = new MainEntities()) {
                    l.roles = dal.sec_users_roles.Include("sec_roles").Where(a => a.user_id == acc.id).Select(b => b.sec_roles.role_key).ToArray();
                }

                return(APIResult <LoginResponse> .Success(l, "API_SUCCESS"));
            }
        }
Ejemplo n.º 2
0
        public APIResult <bool> EditMyProfile(tbl_accounts request)
        {
            using (var ctx = new MainEntities())
            {
                var u   = APIRequest.User(HttpContext.Current.Request);
                var acc = ctx.tbl_accounts.Find(u.Entity.id);
                if (acc == null)
                {
                    return(APIResult <bool> .Error(ResponseCode.DevNotFound, "This account not found!", false));
                }

                acc.first_name        = request.first_name;
                acc.last_name         = request.last_name;
                acc.gender_id         = request.gender_id;
                acc.city_id           = request.city_id;
                acc.driver_license_no = request.driver_license_no;
                acc.id_no             = request.id_no;
                acc.date_of_birth     = request.date_of_birth;
                acc.country_id        = request.country_id;

                ctx.Entry(acc).State = System.Data.Entity.EntityState.Modified;

                try
                {
                    ctx.SaveChanges();
                    return(APIResult <bool> .Success(true));
                }
                catch (Exception ex)
                {
                    return(APIResult <bool> .Error(ResponseCode.BackendDatabase, ex.Message, false));
                }
            }
        }
Ejemplo n.º 3
0
        public ActionResult DeleteConfirmed(int id)
        {
            tbl_accounts tbl_accounts = db.tbl_accounts.Find(id);

            db.tbl_accounts.Remove(tbl_accounts);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
 public ActionResult Edit([Bind(Include = "id,first_name,last_name,date_of_birth,mobile,email,register_time,is_deleted,gender_id,id_no,driver_license_no,city_id,country_id")] tbl_accounts tbl_accounts)
 {
     if (ModelState.IsValid)
     {
         db.Entry(tbl_accounts).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.id         = new SelectList(db.sec_users, "id", "pwd", tbl_accounts.id);
     ViewBag.city_id    = new SelectList(db.tbl_cities, "id", "name", tbl_accounts.city_id);
     ViewBag.country_id = new SelectList(db.tbl_countries, "id", "name", tbl_accounts.country_id);
     ViewBag.gender_id  = new SelectList(db.tbl_genders, "id", "name", tbl_accounts.gender_id);
     return(View(tbl_accounts));
 }
Ejemplo n.º 5
0
        // GET: tbl_accounts/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tbl_accounts tbl_accounts = db.tbl_accounts.Find(id);

            if (tbl_accounts == null)
            {
                return(HttpNotFound());
            }
            return(View(tbl_accounts));
        }
Ejemplo n.º 6
0
        public static APIResult <LoginResponse> Register(tbl_accounts acc, string password, string FirebaseUID, NameValueCollection request)
        {
            using (var ctx = new MainEntities())
            {
                var dbuser = ctx.tbl_accounts.Include("sec_users").Where(a => a.sec_users.firebase_uid == FirebaseUID).FirstOrDefault();
                if (dbuser == null)
                {
                    dbuser = acc;

                    ctx.tbl_accounts.Add(dbuser);
                    try
                    {
                        ctx.SaveChanges();
                        sec_users sec_user = new sec_users();

                        sec_user.pwd            = password;
                        sec_user.id             = dbuser.id;
                        sec_user.mail_verified  = true;
                        sec_user.firebase_uid   = FirebaseUID;
                        sec_user.phone_verified = true;
                        ctx.sec_users.Add(sec_user);
                        ctx.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + "save changes1"));
                    }
                }
                else
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, "This user already exists !"));
                }

                var returned = new LoginResponse {
                    account = acc
                };

                var session = GetNewSession(dbuser.sec_users, request, 1);

                if (session.code != ResponseCode.Success)
                {
                    return(APIResult <LoginResponse> .Error(session.code, session.message));
                }

                returned.token = session.data.id;
                returned.roles = ctx.sec_users_roles.Include("sec_roles").Where(a => a.user_id == acc.id).Select(b => b.sec_roles.role_key).ToArray();

                return(APIResult <LoginResponse> .Success(returned, "Register sucessfuly !"));
            }
        }
Ejemplo n.º 7
0
        public ActionResult SaveEmployee(EmployeeDetails objEmployee)
        {
            //if(ModelState.IsValid)
            // {
            tbl_accounts accOBJ = new tbl_accounts();

            accOBJ.username = objEmployee.username;
            accOBJ.password = objEmployee.paswd;
            accOBJ.status   = "1";
            accOBJ.role     = "employee";
            objentity.tbl_accounts.Add(accOBJ);


            if (objentity.SaveChanges() > 0)
            {
                int id = objentity.tbl_accounts.Max(item => item.id);

                tbl_employeeDtls tblOBJ = new Models.DB.tbl_employeeDtls();
                tblOBJ.emp_id        = objEmployee.emp_id;
                tblOBJ.holder_id     = id;
                tblOBJ.emp_name      = objEmployee.emp_name;
                tblOBJ.department    = objEmployee.department;
                tblOBJ.type          = objEmployee.type;
                tblOBJ.emp_addr      = objEmployee.emp_addr;
                tblOBJ.city          = objEmployee.city;
                tblOBJ.postal_code   = objEmployee.postal_code;
                tblOBJ.state         = objEmployee.state;
                tblOBJ.nationality   = objEmployee.nationality;
                tblOBJ.remarks       = objEmployee.remarks;
                tblOBJ.tel_no        = objEmployee.tel_no;
                tblOBJ.email_id      = objEmployee.email_id;
                tblOBJ.passport_no   = objEmployee.passport_no;
                tblOBJ.visa_status   = objEmployee.visa_status;
                tblOBJ.psprt_expdate = objEmployee.psprt_expdate;
                tblOBJ.visa_date     = objEmployee.visa_date;
                tblOBJ.join_date     = objEmployee.join_date;
                tblOBJ.is_contract   = objEmployee.is_contract;
                tblOBJ.contract_end  = objEmployee.contract_end;
                tblOBJ.is_employee   = objEmployee.is_employee;
                tblOBJ.username      = objEmployee.username;
                tblOBJ.paswd         = objEmployee.paswd;
                objentity.tbl_employeeDtls.Add(tblOBJ);
                objentity.SaveChanges();

                // return RedirectToAction("EmployeeView", "Employee");
            }
            //}
            return(this.RedirectToAction("EmployeeView", "Employee"));
        }
Ejemplo n.º 8
0
        // GET: tbl_accounts/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            tbl_accounts tbl_accounts = db.tbl_accounts.Find(id);

            if (tbl_accounts == null)
            {
                return(HttpNotFound());
            }
            ViewBag.id         = new SelectList(db.sec_users, "id", "pwd", tbl_accounts.id);
            ViewBag.city_id    = new SelectList(db.tbl_cities, "id", "name", tbl_accounts.city_id);
            ViewBag.country_id = new SelectList(db.tbl_countries, "id", "name", tbl_accounts.country_id);
            ViewBag.gender_id  = new SelectList(db.tbl_genders, "id", "name", tbl_accounts.gender_id);
            return(View(tbl_accounts));
        }
Ejemplo n.º 9
0
        public ActionResult AssginVehcileToDriver(FormCollection form)
        {
            var vehcile_id = int.Parse(form.Get("vehicle_id") == null?"0": form.Get("vehicle_id"));
            var driver_id  = int.Parse(form.Get("account_id") == null?"0": form.Get("account_id"));

            tbl_drivers_vehicles_rel rel = new tbl_drivers_vehicles_rel()
            {
                vehicle_id = vehcile_id,
                driver_id  = driver_id,
                created_at = DateTime.Now,
                created_by = UserSession.User.user_id,
                status     = 0,
            };

            db.tbl_drivers_vehicles_rel.Add(rel);

            if (db.SaveChanges() <= 0)
            {
                return(Json(new { type = "error", message = "" }));
            }
            tbl_accounts driver = db.tbl_accounts.Include(t => t.sec_users).FirstOrDefault(a => a.id == driver_id);

            return(Json(new { type = "success", message = "Your request sent to " + driver.first_name + " " + driver.last_name + ", Please wait for his action" }));
        }
Ejemplo n.º 10
0
        public async Task <APIResult <LoginResponse> > Register(RegisterRequest request)
        {
            var c = HttpContext.Current;
            var v = request.isValid();

            if (v.data == false)
            {
                return(APIResult <LoginResponse> .Error(v.code, v.message));
            }


            string trueMobile = "";

            if (General.ValidateMobile(request.phoneNumber, out trueMobile))
            {
                request.phoneNumber = trueMobile;
            }
            else
            {
                return(APIResult <LoginResponse> .Error(ResponseCode.UserValidationField, "Invalid mobile number!"));
            }

            var verified = Users.MobileVerified(request.phoneNumber, request.verification_id);

            if (!verified.data)
            {
                return(APIResult <LoginResponse> .Error(ResponseCode.UserUnVerified, "Your phone still not verified!"));
            }

            #region Get User Data From Firebase

            try
            {
                if (FirebaseApp.DefaultInstance != null)
                {
                    FirebaseApp.DefaultInstance.Delete();
                }

                //{

                FirebaseApp.Create(new AppOptions()
                {
                    Credential = GoogleCredential.FromFile(c.Server.MapPath("~/App_Data/firebase-config.json")),
                }
                                   );
            }
            catch (Exception ex)
            {
                throw new Exception(ErrorHandler.Message(ex));
            }
            FirebaseToken decodedToken;
            try
            {
                decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(request.access_token);
            }
            catch (Exception ex)
            {
                throw new Exception(ErrorHandler.Message(ex));
            }

            string uid   = decodedToken.Uid;
            string email = "";
            string phone = request.phoneNumber;

            try
            {
                if (decodedToken.Claims.Keys.Contains("email"))
                {
                    email = decodedToken.Claims.FirstOrDefault(a => a.Key == "email").Value.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ErrorHandler.Message(ex));
            }

            #endregion

            var f_name = request.first_name;

            var l_name = request.last_name;



            if (email == "")
            {
                return(APIResult <LoginResponse> .Error(ResponseCode.UserValidationField, "Email is required!"));
            }
            if (phone == "")
            {
                return(APIResult <LoginResponse> .Error(ResponseCode.UserValidationField, "Phone is required!"));
            }
            tbl_accounts acc = new tbl_accounts();
            acc.city_id       = request.city == 0?null:request.city;
            acc.country_id    = request.country;
            acc.date_of_birth = request.date_of_birth;
            acc.email         = email;
            acc.first_name    = request.first_name;
            acc.gender_id     = request.gender_id;
            acc.last_name     = request.last_name;
            acc.mobile        = phone;
            acc.register_time = DateTime.Now;


            return(Users.Register(acc, request.password, uid, c.Request.ServerVariables));
        }
Ejemplo n.º 11
0
        public async Task <APIResult <LoginResponse> > Auth(string email, string password, string first_name, string last_name, HttpContext http, string pic = "", string network = "", string FirebaseUID = "")
        {
            using (DAL.MainEntities ctx = new DAL.MainEntities())
            {
                //try
                //{


                tbl_accounts dbuser = null;
                try
                {
                    if (email != "")
                    {
                        dbuser = ctx.tbl_accounts.Include("sec_users").Where(a => a.email == email).FirstOrDefault();
                    }
                    else
                    if (FirebaseUID != "")
                    {
                        dbuser = ctx.tbl_accounts.Include("sec_users").Where(a => a.sec_users.firebase_uid == FirebaseUID).FirstOrDefault();
                    }

                    if (dbuser == null)
                    {
                        dbuser               = new tbl_accounts();
                        dbuser.email         = email;
                        dbuser.first_name    = first_name;
                        dbuser.last_name     = last_name;
                        dbuser.register_time = DateTime.Now;


                        ctx.tbl_accounts.Add(dbuser);
                        try
                        {
                            ctx.SaveChanges();
                            sec_users sec_user = new sec_users();

                            sec_user.pwd           = password;
                            sec_user.id            = dbuser.id;
                            sec_user.mail_verified = true;

                            if (FirebaseUID != "")
                            {
                                sec_user.firebase_uid = FirebaseUID;
                            }

                            ctx.sec_users.Add(sec_user);
                            ctx.SaveChanges();
                        }
                        //catch (DbEntityValidationException e)
                        //{
                        //    return new APIResult<LoginResponse>(ResultType.fail, null, General.fetchEntityError(e));
                        //}
                        catch (Exception ex)
                        {
                            return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + "save changes1"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + "get dbuser"));
                }
                tbl_images img = ctx.tbl_images.Where(a => a.model_name == "tbl_accounts" && a.model_id == dbuser.id && a.model_tag == "main").FirstOrDefault();
                if (pic != "" && img == null)
                {
                    img = new tbl_images();

                    try
                    {
                        img.original = "/Storage/Original/" + DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + network + ".jpg";
                        string imgPath = ConfigurationManager.AppSettings["mediaServer_Path"] + img.original.Replace("/", "\\");
                        img.large      = img.original;
                        img.thumb      = img.original;
                        img.meduim     = img.original;
                        img.model_id   = dbuser.id;
                        img.model_name = "tbl_accounts";
                        img.model_tag  = "main";
                        System.Net.WebClient webClient = new System.Net.WebClient();

                        webClient.Encoding = System.Text.Encoding.UTF8;


                        webClient.DownloadFile(pic, imgPath);
                        ctx.tbl_images.Add(img);
                    }
                    catch (Exception ex)
                    {
                        // return APIResult<LoginResponse>.(ResultType.fail, null, ex.Message + "Save Image");
                    }
                    try
                    {
                        ctx.SaveChanges();
                    }
                    //catch (DbEntityValidationException e)
                    //{

                    //    return new APIResult<LoginResponse>(ResultType.fail, null, General.fetchEntityError(e));
                    //}
                    catch (Exception ex)
                    {
                        // return new APIResult<LoginResponse>(ResultType.fail, null, ex.Message + "save changes2");
                    }
                }



                var returned = new LoginResponse {
                    account = dbuser
                };
                IPResult s = new IPResult();

                string   ip    = "";
                string   agent = "";
                IPResult iploc = new IPResult();


                //if(HttpContext.Current == null) return new APIResult<LoginResponse>(ResultType.fail, null, "Null HTTPContext");
                //if (http.Request == null) return  APIResult<LoginResponse>.Error(ResponseCode., null, "Null HTTPRequest");
                //if (http.Request.ServerVariables == null) return new APIResult<LoginResponse>(ResultType.fail, null, "Null ServerVariables");
                //if (http.Request.ServerVariables.Count == 0) return new APIResult<LoginResponse>(ResultType.fail, null, "Empty ServerVariables");
                //if (!http.Request.ServerVariables.AllKeys.Contains("REMOTE_ADDR")) return new APIResult<LoginResponse>(ResultType.fail, null, "REMOTE_ADDR Not in ServerVariables");
                //if (!http.Request.ServerVariables.AllKeys.Contains("HTTP_USER_AGENT")) return new APIResult<LoginResponse>(ResultType.fail, null, "HTTP_USER_AGENT No in ServerVariables");
                try
                {
                    ip    = http.Request.ServerVariables.Get("REMOTE_ADDR");
                    agent = http.Request.ServerVariables.Get("HTTP_USER_AGENT");

                    iploc = General.GetResponse("http://ip-api.com/json/" + ip);
                }
                catch (Exception ex)
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.BackendServerRequest, ex.Message + "get location ip:" + ip + " agent:" + agent));
                }

                try
                {
                    //&& a.ip == ip && a.agent == agent
                    var userSessions = ctx.sec_sessions.Where(a => a.user_id == dbuser.id && a.end_time == null).FirstOrDefault();
                    if (userSessions == null)
                    {
                        Sessions ses = new Sessions();
                        ses.Entity.user_id      = dbuser.id;
                        ses.Entity.ip           = ip;
                        ses.Entity.isp          = iploc.isp;
                        ses.Entity.lat          = iploc.lat;
                        ses.Entity.lon          = iploc.lon;
                        ses.Entity.timezone     = iploc.timezone;
                        ses.Entity.city         = iploc.city;
                        ses.Entity.country      = iploc.country;
                        ses.Entity.country_code = iploc.countryCode;
                        ses.Entity.agent        = agent;


                        ctx.sec_sessions.Add(ses.Entity);
                        ctx.SaveChanges();

                        dbuser.sec_users.sec_sessions = new List <sec_sessions>()
                        {
                            ses.Entity
                        };
                        returned.token = ses.Entity.id;
                    }
                    else
                    {
                        returned.token = userSessions.id;
                    }

                    returned.roles = ctx.sec_users_roles.Include("sec_roles").Where(a => a.user_id == dbuser.id).Select(b => b.sec_roles.role_key).ToArray();
                    return(APIResult <LoginResponse> .Success(returned, "Login Success"));
                }
                catch (DbEntityValidationException e)
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, General.fetchEntityError(e)));
                }
                catch (Exception ex)
                {
                    return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + " Save Session"));
                }

                //}
                //catch (Exception ex)
                //{

                //    throw new Exception( ex.Message + "Auth");
                //}
            }
        }