Esempio n. 1
0
        public static caUser CurrentUser()
        {
            caUser User = new caUser();

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

            return(User);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }