Ejemplo n.º 1
0
        public IHttpActionResult SendEmail(EmailModel emailModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Not a valid model"));
                }
                else
                {
                    var config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap <EmailModel, EmailDTO>();
                    });
                    IMapper mapper = config.CreateMapper();

                    var emailResultData = mapper.Map <EmailModel, EmailDTO>(emailModel);

                    string powerBIUserName = ConfigurationManager.AppSettings["powerBIUserName"];
                    string powerBIPWD      = ConfigurationManager.AppSettings["powerBIPWD"];
                    string smtpHost        = ConfigurationManager.AppSettings["smtpHost"];
                    int    smtpPort        = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);

                    EmailData emailData = new EmailData();

                    emailData.SendEmail(powerBIUserName, powerBIPWD, smtpHost, smtpPort, emailResultData.ToEmail, emailResultData.Subject, emailResultData.Body);

                    return(Created("api/SendEmail", true));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest("Send email failed"));
            }
        }
Ejemplo n.º 2
0
        //Add a new user to a workspace
        public string AddPowerBIWorkspaceUser(List <WorkFlowMasterDTO> workFlowMasterDTO, string powerBIUserName, string powerBIPWD, string smtpHost, int smtpPort)
        {
            //// Create the InitialSessionState Object
            //InitialSessionState iss = InitialSessionState.CreateDefault2();

            //// Initialize PowerShell Engine
            //var shell = PowerShell.Create(iss);
            //shell.Commands.AddCommand("Connect-PowerBIServiceAccount");

            //System.Security.SecureString theSecureString = new NetworkCredential(powerBIUserName, powerBIPWD).SecurePassword;
            //PSCredential cred = new PSCredential(powerBIUserName, theSecureString);

            //shell.Commands.AddParameter("Credential", cred);

            //var results = shell.Invoke();
            //if (results.Count > 0)
            //{
            using (var context = new BIPortalEntities())
            {
                foreach (var a in workFlowMasterDTO)
                {
                    var workflowMasterEntity = context.WorkFlowMasters.FirstOrDefault(x => x.RequestID == a.RequestID);
                    if (workflowMasterEntity != null)
                    {
                        // Initialize PowerShell Engine
                        //var shell2 = PowerShell.Create(iss);
                        //shell2.Commands.AddCommand("Add-PowerBIWorkspaceUser");
                        //shell2.Commands.AddParameter("Id", workflowMasterEntity.WorkspaceID);
                        //shell2.Commands.AddParameter("UserEmailAddress", workflowMasterEntity.RequestFor);
                        //shell2.Commands.AddParameter("AccessRight", "Member");
                        //var resaddUser = shell2.Invoke();

                        GroupUser userdetails = new GroupUser();
                        userdetails.EmailAddress         = workflowMasterEntity.RequestFor;
                        userdetails.GroupUserAccessRight = "Member";
                        var groupId          = Guid.Parse(workflowMasterEntity.WorkspaceID);
                        var accessToken      = a.PowerBIAccessToken;
                        var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
                        var pbiClient        = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials);
                        // You need to provide the workspaceId where the dashboard resides.
                        pbiClient.Groups.AddGroupUser(groupId, userdetails);

                        //send email
                        var       subject   = "Your request is approved.";
                        var       body      = "Your request for access to workspace " + workflowMasterEntity.WorkspaceName + " has been approved.";
                        EmailData emailData = new EmailData();
                        emailData.SendEmail(powerBIUserName, powerBIPWD, smtpHost, smtpPort, workflowMasterEntity.RequestFor, subject, body);
                    }
                }
                //}
            }
            return("User added to workspace successfully");
        }
Ejemplo n.º 3
0
        public string SendEmail(string subject, string body, string toAddresses, string attachmentFileLocations)
        {
            string retVal = "SUCCESS";

            try
            {
                EmailData.SendEmail(subject, body, toAddresses, attachmentFileLocations);
            }
            catch (Exception ex)
            {
                string errorMsg = ex.Message;
                retVal = "FAILURE";
            }

            return(retVal);
        }
Ejemplo n.º 4
0
        public void RejectRequest(List <WorkFlowMasterDTO> workFlowMasterDTO, string powerBIUserName, string powerBIPWD, string smtpHost, int smtpPort)
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <WorkFlowMasterDTO, WorkFlowMaster>();
                cfg.CreateMap <WorkFlowDetailsDTO, WorkFlowDetail>();
            });
            IMapper mapper = config.CreateMapper();

            var workFlowMasterDetails = mapper.Map <List <WorkFlowMasterDTO>, List <WorkFlowMaster> >(workFlowMasterDTO);

            using (var context = new BIPortalEntities())
            {
                foreach (var f in workFlowMasterDTO)
                {
                    var workflowMasterEntity = context.WorkFlowMasters.FirstOrDefault(x => x.RequestID == f.RequestID);
                    if (workflowMasterEntity != null)
                    {
                        workflowMasterEntity.Status        = f.Status;
                        workflowMasterEntity.ProcessedDate = f.ProcessedDate;

                        context.WorkFlowMasters.AddOrUpdate(workflowMasterEntity);
                        context.SaveChanges();

                        //send email
                        var       subject   = "Your request is rejected.";
                        var       body      = "Your request for access to workspace " + workflowMasterEntity.WorkspaceName + " has been rejected.";
                        EmailData emailData = new EmailData();
                        emailData.SendEmail(powerBIUserName, powerBIPWD, smtpHost, smtpPort, workflowMasterEntity.RequestFor, subject, body);
                    }
                    foreach (var e in f.WorkFlowDetails)
                    {
                        var workflowDetailEntity = context.WorkFlowDetails.FirstOrDefault(x => x.RequestID == e.RequestID && x.ReportID == e.ReportID);
                        if (workflowDetailEntity != null)
                        {
                            workflowDetailEntity.Status        = e.Status;
                            workflowDetailEntity.ProcessedDate = e.ProcessedDate;

                            context.WorkFlowDetails.AddOrUpdate(workflowDetailEntity);
                            context.SaveChanges();
                        }
                    }
                }
            }
        }