コード例 #1
0
        private int SavePolicy()         //change to int
        {
            int PolicyID;
            int NewPolicyID;

            try
            {
                PolicyID    = Int32.Parse(Session["PolicyID"].ToString());
                NewPolicyID = Int32.Parse(Session["PolicyID"].ToString());
            }
            catch
            {
                PolicyID    = -1;
                NewPolicyID = -1;
            }

            //Get values
            int    OrganisationID      = UserContext.UserData.OrgID;
            string PolicyName          = this.txtName.Text;
            string ConfirmationMessage = this.txtConfirmationMsg.Text;
            bool   Active   = (this.ddlStatus.SelectedIndex == 0);
            bool   Deleted  = false;
            string FileName = this.UploadFile.PostedFile.FileName;


            // Create Policy object
            BusinessServices.Policy objPolicy = new BusinessServices.Policy();

            if (PolicyID > 0)
            {
                //Existing Policy - updating
                objPolicy.UpdatePolicy(PolicyID, OrganisationID, PolicyName, Active, ConfirmationMessage);

                //Check to see if new file needs to be uploaded
                if (FileName.Length > 0)
                {
                    NewPolicyID = Upload(PolicyID, true);
                }
                if (this.chkAssignAllUsers.Checked)
                {
                    objPolicy.InitialisePolicyAccess(OrganisationID, NewPolicyID, this.chkAssignAllUsers.Checked);
                }
                Session["PolicyID"] = NewPolicyID;
                return(NewPolicyID);
            }
            else
            {
                if (FileName.Length > 0)
                {
                    string    PolicyFileName = Path.GetFileName(FileName);
                    DataTable dt             = objPolicy.CheckFileName(OrganisationID, PolicyFileName);
                    int       CheckRowCount  = dt.Rows.Count;
                    if (CheckRowCount == 0)
                    {
                        //Check if the file is less than 2 mb
                        long PolicyFileSize = GetFileSize();
                        //New Policy - adding
                        PolicyID    = objPolicy.AddPolicy(OrganisationID, PolicyName, Active, Deleted, PolicyFileName, PolicyFileSize, ConfirmationMessage);
                        NewPolicyID = Upload(PolicyID, false);
                        objPolicy.InitialisePolicyAccess(OrganisationID, NewPolicyID, this.chkAssignAllUsers.Checked);
                        Session["PolicyID"] = NewPolicyID;
                        return(NewPolicyID);
                    }
                    else
                    {
                        lblUploadFile.Text     = ResourceManager.GetString("FileExists");
                        lblUploadFile.CssClass = "WarningMessage";
                        return(-1);
                    }
                }
                else
                {
                    //File has not been specified for a new policy - prompt user to set file to upload
                    lblUploadFile.Text     = ResourceManager.GetString("NoUploadFile");
                    lblUploadFile.CssClass = "WarningMessage";
                    return(-1);
                }
            }
        }
コード例 #2
0
        private bool UploadPolicyFile(bool checkFile)
        {
            bool UploadStatus = false;

            // Create Policy object
            BusinessServices.Policy objPolicy = new BusinessServices.Policy();

            //Check value in the upload file input
            if ((UploadFile.PostedFile != null) && (UploadFile.PostedFile.ContentLength > 0))
            {
                //Get filename of file to be uploaded
                string FileName      = GetFileName();
                int    CheckRowCount = 0;

                //code to check the database for a filename that has been previously used
                //If it exists then display message indicating that filenames need to be unique and do not allow upload of file
                int OrganisationID = UserContext.UserData.OrgID;

                if (checkFile)
                {
                    DataTable dt = objPolicy.CheckFileName(OrganisationID, FileName);
                    CheckRowCount = dt.Rows.Count;
                }

                if (CheckRowCount != 0 && checkFile)
                {
                    // File with this name already exists for this organisation
                    lblUploadFile.Text     = ResourceManager.GetString("FileExists");
                    lblUploadFile.CssClass = "WarningMessage";
                    CheckCanUpload         = false;
                }
                else
                {
                    //Get fileinfo of file to be uploaded - length() returns file size in bytes
                    long FileLength = GetFileSize();

                    //Obtain the size limit the organisation has
                    long lngOrgSizeLimit = objPolicy.GetAllocatedDiskSpace(OrganisationID);
                    if (lngOrgSizeLimit == 0)
                    {
                        // No value returned - Organisation has not had size allocated in organisation details - message to user
                        lblUploadFile.Text     = ResourceManager.GetString("OrgNoSpaceAllocated");
                        lblUploadFile.CssClass = "WarningMessage";
                    }
                    else
                    {
                        //Determine the size of files already on server - note that at this stage this includes the file you are currently uploading
                        long lngOrgUsedLimit = objPolicy.GetUsedPolicyDiskSpace(OrganisationID);

                        //check that size of files already on server + new file size does not exceed the size limit for organisation.
                        //If it does then do not upload the file and get them to contact SALT administrator
                        CheckCanUpload = objPolicy.CheckCanUpload(lngOrgSizeLimit, lngOrgUsedLimit);
                        if (CheckCanUpload)
                        {
                            //Location for file to be saved on server
                            string SaveDir = Server.MapPath(@"\General") + @"\Policy\" + UserContext.UserData.OrgID.ToString();
                            //Check that the directory exists - if it doesn't then create it
                            if (!Directory.Exists(SaveDir))
                            {
                                Directory.CreateDirectory(SaveDir);
                            }
                            string SaveLocation = SaveDir + @"\" + FileName;

                            try
                            {
                                UploadFile.PostedFile.SaveAs(SaveLocation);
                                lblUploadFile.Text     = ResourceManager.GetString("UploadSuccess");
                                lblUploadFile.CssClass = "SuccessMessage";
                                UploadStatus           = true;
                            }
                            catch (Exception ex)
                            {
                                //log exception to event log
                                ErrorHandler.ErrorLog el = new ErrorHandler.ErrorLog(ex, ErrorLevel.High, "PolicyDetails.aspx.cs", "UploadPolicyFile", ex.Message);

                                //display friendly message to user
                                lblUploadFile.Text     = ResourceManager.GetString("UploadFail");
                                lblUploadFile.CssClass = "WarningMessage";
                            }
                        }
                        else
                        {
                            // Organisation allocated size will be exceeded - message to tell user this
                            lblUploadFile.Text     = ResourceManager.GetString("ExceedOrgSizeAllocation");
                            lblUploadFile.CssClass = "WarningMessage";
                        }
                    }
                }
            }
            else
            {
                lblUploadFile.Text     = ResourceManager.GetString("NoUploadFile");
                lblUploadFile.CssClass = "WarningMessage";
            }
            return(UploadStatus);
        }