예제 #1
0
        public bool AuthoriseUser(string uname, string upass)
        {
            IApplicationContext context = DependencyResolver.Current.GetService <IApplicationContext>();

            context.Configuration.ProxyCreationEnabled = false;
            context.Configuration.LazyLoadingEnabled   = false;

            upass = GaneStaticAppExtensions.GetMd5(upass);

            if (HttpContext.Current.Session["caTenant"] == null)
            {
                // set error details
                caError error = new caError();
                error.ErrorTtile   = "Client not validated";
                error.ErrorMessage = "Sorry, system is unable to validate client";
                error.ErrorDetail  = "Either client is not registered, inactive or ambiguous, please contact support";

                HttpContext.Current.Session["caError"]  = error;
                HttpContext.Current.Session["ErrorUrl"] = "~/error";
            }

            else
            {
                caTenant tenant = (caTenant)HttpContext.Current.Session["caTenant"];
                TenantId = tenant.TenantId;
            }

            var Users = context.AuthUsers.AsNoTracking().Where(e => e.UserName.Equals(uname, StringComparison.CurrentCultureIgnoreCase) && e.UserPassword == upass.Trim() && e.TenantId == TenantId && e.IsActive && e.IsDeleted != true)
                        .Include(x => x.AuthPermissions.Select(y => y.AuthActivity))
                        .ToList();

            if (Users.Any() && Users.Count() < 2)
            {
                var user = Users.FirstOrDefault();

                UserId          = user.UserId;
                UserName        = user.UserName;
                UserFirstName   = user.UserFirstName;
                UserLastName    = user.UserLastName;
                UserEmail       = user.UserEmail;
                DateCreated     = user.DateCreated;
                DateUpdated     = user.DateUpdated;
                CreatedBy       = user.CreatedBy;
                UpdatedBy       = user.UpdatedBy;
                IsActive        = user.IsActive;
                IsDeleted       = user.IsDeleted;
                TenantId        = user.TenantId;
                AuthPermissions = user.AuthPermissions;
                SuperUser       = user.SuperUser;
                UserCulture     = user.UserCulture;
                UserTimeZoneId  = user.UserTimeZoneId;

                AuthUserStatus = true;
            }

            return(AuthUserStatus);
        }
예제 #2
0
        public static caTenant CurrentTenant()
        {
            caTenant tenant = new caTenant();

            if (HttpContext.Current.Session["caTenant"] != null)
            {
                tenant = (caTenant)HttpContext.Current.Session["caTenant"];
            }
            return(tenant);
        }
예제 #3
0
        // log application errors

        public Boolean ErrorLogWriter()
        {
            Boolean status = false;

            int    TenantId   = 0;
            int    UserId     = 0;
            string TenantName = "";
            string UserName   = "";
            string SubDoamin  = "";

            if (HttpContext.Current.Session["caError"] != null)
            {
                caError error = (caError)HttpContext.Current.Session["caError"];
                ErrorTtile      = error.ErrorTtile;
                ErrorMessage    = error.ErrorMessage;
                ErrorDetail     = error.ErrorDetail;
                ErrorController = error.ErrorController;
                ErrorAction     = error.ErrorAction;

                if (HttpContext.Current.Session["caTenant"] != null)
                {
                    // current tenant id, name and subdomain form session
                    caTenant tenant = (caTenant)HttpContext.Current.Session["caTenant"];
                    TenantId   = tenant.TenantId;
                    TenantName = tenant.TenantName;
                    SubDoamin  = tenant.TenantSubDmoain;
                }

                if (HttpContext.Current.Session["caUser"] != null)
                {
                    // get properties of user
                    caUser user = (caUser)HttpContext.Current.Session["caUser"];
                    UserId   = user.UserId;
                    UserName = user.UserName;
                }

                // write in error log file using string builder and stream writer
                StringBuilder builder = new StringBuilder();
                builder
                .AppendLine("Date/Time: " + DateTime.UtcNow.ToString())
                .AppendLine("Tenant Name: " + TenantName + "( Teanant Id: " + TenantId + ")")
                .AppendLine("User: "******"( Id: " + UserId + ")")
                .AppendLine("Controller: " + ErrorController)
                .AppendLine("Action: " + ErrorAction)
                .AppendLine("Error Title: " + ErrorTtile)
                .AppendLine("Error Message: " + ErrorMessage)
                .AppendLine("Error Detail: " + ErrorDetail)
                .AppendLine("-----------------------------------------------")
                .Append(Environment.NewLine);


                // get log preferences Local | Azure | Both
                int LogWriting = Convert.ToInt32(ConfigurationManager.AppSettings.Get("LogWriting"));

                if (LogWriting != 0)
                {
                    if (LogWriting == 1 || LogWriting == 3)
                    {
                        //*************************** File Writing for Local Storage *****************************************

                        string filePath = HttpContext.Current.Server.MapPath("~/Logs/Error.log");

                        using (StreamWriter writer = File.AppendText(filePath))
                        {
                            writer.Write(builder.ToString());
                            writer.Flush();
                        }
                    }

                    //if (LogWriting == 2 || LogWriting == 3)
                    //{
                    //    //*************************** Blob Writing for Azure  Hosting only *****************************************

                    //    // Retrieve storage account from connection string.
                    //    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    //         CloudConfigurationManager.GetSetting("StorageConnectionString"));

                    //    // Create the blob client.
                    //    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    //    // Retrieve a reference to a container.
                    //    CloudBlobContainer container = blobClient.GetContainerReference("logs");

                    //    CloudBlockBlob blob = container.GetBlockBlobReference("Error.log");
                    //    string contents = builder.ToString(); /* content to append */

                    //    if (blob.Exists())
                    //    {
                    //        using (Stream blobStream = blob.OpenRead())
                    //        {
                    //            byte[] buffer = new byte[4096];
                    //            using (Stream tempBlobStream = blob.OpenWrite())
                    //            {
                    //                int read;
                    //                while ((read = blobStream.Read(buffer, 0, 4096)) > 0)
                    //                {
                    //                    tempBlobStream.Write(buffer, 0, read);
                    //                }

                    //                using (StreamWriter writer = new StreamWriter(tempBlobStream))
                    //                {
                    //                    writer.Write(contents);
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
                }

                status = true;
            }

            return(status);
        }
예제 #4
0
        public static bool AuthoriseSession()
        {
            bool   Status             = false;
            string ControllerName     = "";
            string ActionName         = "";
            int    CurrentWarehouseId = 0;

            HttpContext.Current.Session["ErrorUrl"] = "~/error";

            var context = DependencyResolver.Current.GetService <IApplicationContext>();

            var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;

            if (routeValues != null)
            {
                if (routeValues.ContainsKey("action"))
                {
                    ControllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
                    ActionName     = HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString();
                }
            }

            if (HttpContext.Current.Session["caTenant"] == null)
            {
                Uri      Url = HttpContext.Current.Request.Url;
                caTenant ca  = new caTenant();

                if (ca.AuthorizeTenant(Url) == true)
                {
                    HttpContext.Current.Session["caTenant"] = ca;
                }
            }


            // check conditions

            if (HttpContext.Current.Session["caTenant"] == null)
            {
                // set error details
                caError error = new caError();
                error.ErrorTtile      = "Client not validated";
                error.ErrorMessage    = "Sorry, system is unable to validate client";
                error.ErrorDetail     = "Either client is not registered, inactive or ambiguous, please contact support";
                error.ErrorController = ControllerName;
                error.ErrorAction     = ActionName;

                HttpContext.Current.Session["caError"]  = error;
                HttpContext.Current.Session["ErrorUrl"] = "~/error";
            }

            else
            {
                if (HttpContext.Current.Session["caUser"] == null)
                {
                    HttpContext.Current.Session["ErrorUrl"] = "~/user/login";
                    if (HttpContext.Current.Session["LastUrlFrom"] == null)
                    {
                        HttpContext.Current.Session["LastUrlFrom"] = HttpContext.Current.Request.RawUrl;
                    }
                }
                else
                {
                    caTenant tenant = (caTenant)HttpContext.Current.Session["caTenant"];
                    caUser   user   = (caUser)HttpContext.Current.Session["caUser"];

                    if (tenant.TenantId.Equals(user.TenantId))
                    {
                        if (user.SuperUser == true)
                        {
                            if (HttpContext.Current.Session["CurrentWarehouseId"] == null)
                            {
                                HttpContext.Current.Session["CurrentWarehouseId"] = tenant.TenantLocations.FirstOrDefault().WarehouseId;
                            }
                            Status = true;
                            return(Status);
                        }

                        if (HttpContext.Current.Session["CurrentWarehouseId"] == null)
                        {
                            if (user.AuthPermissions.Any())
                            {
                                CurrentWarehouseId = user.AuthPermissions.FirstOrDefault().WarehouseId;
                            }
                            HttpContext.Current.Session["CurrentWarehouseId"] = CurrentWarehouseId;
                        }
                        else
                        {
                            CurrentWarehouseId = (int)HttpContext.Current.Session["CurrentWarehouseId"];
                        }

                        ICollection <AuthActivity> CurrentActivity = context.AuthActivities.AsNoTracking().Where(e => e.ActivityController.Trim().ToLower() == ControllerName.Trim().ToLower() &&
                                                                                                                 e.ActivityAction.Trim().ToLower() == ActionName.Trim().ToLower() && e.IsActive == true && e.IsDeleted != true).ToList();

                        if (CurrentActivity.Count() == 0 || CurrentActivity.Count() > 1)
                        {
                            caError error = new caError();

                            if (CurrentActivity.Count() == 0)
                            {
                                error.ErrorTtile      = "No authorisation for requested resources";
                                error.ErrorMessage    = "Sorry, requested activity is not registered, and cannot be allowed to view";
                                error.ErrorDetail     = "Problem getting activity, Activity is not registerd or inactive";
                                error.ErrorController = ControllerName;
                                error.ErrorAction     = ActionName;
                            }

                            if (CurrentActivity.Count() > 1)
                            {
                                error.ErrorTtile      = "No authorisation for requested resources";
                                error.ErrorMessage    = "Sorry, Duplicate Entry for This Activity";
                                error.ErrorDetail     = "Activity is found more then once, please contact support for assistence";
                                error.ErrorController = ControllerName;
                                error.ErrorAction     = ActionName;
                            }

                            HttpContext.Current.Session["caError"]  = error;
                            HttpContext.Current.Session["ErrorUrl"] = "~/error";
                        }

                        else
                        {
                            int ThisActivity = CurrentActivity.First().ActivityId;
                            ICollection <AuthPermission> permissons = user.AuthPermissions;

                            if (permissons.Any(c => c.ActivityId == ThisActivity && c.WarehouseId == CurrentWarehouseId && c.IsActive == true &&
                                               c.IsDeleted == false) || CurrentActivity.First().ExcludePermission == true)
                            {
                                Status = true;

                                if (user.AuthUserStatus)
                                {
                                    AuthUserLoginActivity LoginActivity = new AuthUserLoginActivity();

                                    LoginActivity.ActivityId  = ThisActivity;
                                    LoginActivity.UserLoginId = (int)HttpContext.Current.Session["CurrentUserLoginId"];
                                    LoginActivity.WarehouseId = CurrentWarehouseId;
                                    LoginActivity.DateCreated = DateTime.UtcNow;
                                    LoginActivity.TenantId    = tenant.TenantId;

                                    context.AuthUsersLoginActivities.Add(LoginActivity);
                                    context.SaveChanges();
                                }
                            }
                            else
                            {
                                caError error = new caError();
                                error.ErrorTtile      = "No authorisation for requested resources";
                                error.ErrorMessage    = "Sorry, you dont have permissions to access this activity, Please contact Admin";
                                error.ErrorDetail     = "This Activity is not authorised for current User";
                                error.ErrorController = ControllerName;
                                error.ErrorAction     = ActionName;
                                HttpContext.Current.Session["caError"]  = error;
                                HttpContext.Current.Session["ErrorUrl"] = "~/error";
                            }
                        }
                    }
                    else
                    {
                        caError error = new caError();
                        error.ErrorTtile      = "Unable to validate user against client";
                        error.ErrorMessage    = "Sorry, system is unable to validate user against client";
                        error.ErrorDetail     = "System cannot verify user association with client. Please contact sypport.";
                        error.ErrorController = ControllerName;
                        error.ErrorAction     = ActionName;
                        HttpContext.Current.Session["caError"]  = error;
                        HttpContext.Current.Session["ErrorUrl"] = "~/error";
                    }
                }
            }

            return(Status);
        }