public async Task <ActionResult> CreateGuestUser(GuestUserModel _objGstUsrMdl)
        {
            ResultsViewModel results = new ResultsViewModel(false);

            if (ModelState.IsValid)
            {
                try
                {
                    var graphServiceClient = GraphAuthService.CreateGraphServiceClient(ConfigHelper.UseApplicationPermissions);

                    string inviteURL = ConfigHelper.inviteRedirectURLBase;// + ConfigHelper.Tenant;

                    Tuple <bool, string, string> tup = await usersService.CreateGuestUser(graphServiceClient, _objGstUsrMdl.UserDisplayName, _objGstUsrMdl.UserEmailAddress,
                                                                                          inviteURL, _objGstUsrMdl.WelcomeMessage);

                    _objGstUsrMdl.userID           = tup.Item2;
                    _objGstUsrMdl.resultantMessage = tup.Item3;
                    _objGstUsrMdl.status           = tup.Item1;

                    return(View("GuestUsersCreateResult", _objGstUsrMdl));
                }

                catch (Exception ex)
                {
                    _objGstUsrMdl.status           = false;
                    _objGstUsrMdl.resultantMessage = ex.Message + (ex.InnerException != null ? Environment.NewLine + ex.InnerException.Message : "");
                }
            }
            return(View("GuestUsers", _objGstUsrMdl));
        }
        public async Task <ActionResult> GetAccessReviewDetails(string typeid, string id)
        {
            string formattedJson             = "";
            string status                    = "ERROR";
            string message                   = "Could not complete the request";
            Tuple <bool, string, string> res = new Tuple <bool, string, string>(false, "", "");
            List <string> lstInstances       = new List <string>();

            try
            {
                var graphServiceClient = GraphAuthService.CreateGraphServiceClient(ConfigHelper.UseApplicationPermissions);

                string token = GraphAuthService.GetTokenForApplication(ConfigHelper.UseApplicationPermissions, false).Result;

                if (typeid == "1")
                {
                    res = await usersService.GetAccessReviewDetails(graphServiceClient, token, ConfigHelper.GraphUrl, id);
                }
                else if (typeid == "2")
                {
                    res = await usersService.GetAccessReviewReviewers(graphServiceClient, token, ConfigHelper.GraphUrl, id);
                }
                else if (typeid == "3")
                {
                    res = await usersService.GetAccessReviewDecisions(graphServiceClient, token, ConfigHelper.GraphUrl, id);
                }

                if (res.Item1)
                {
                    formattedJson = res.Item3;// JsonConvert.SerializeObject(res.Item3, Formatting.Indented);
                    status        = "OK";
                }
                else
                {
                    message = res.Item2;
                }
            }
            catch (Exception ex)
            {
                message = ex.Message + " " + (ex.InnerException != null ? ex.InnerException.Message : "");
            }

            var jsonData = new
            {
                status  = status,
                message = message,
                jresult = formattedJson
            };

            return(Json(jsonData, JsonRequestBehavior.AllowGet));
        }
        public ViewResult GuestUsers()
        {
            Tuple <bool, string, List <Microsoft.Graph.User> > results = new Tuple <bool, string, List <Microsoft.Graph.User> >(false, string.Empty, null);

            try
            {
                var graphServiceClient = GraphAuthService.CreateGraphServiceClient(ConfigHelper.UseApplicationPermissions);
                results = usersService.GetGuestUsers(graphServiceClient).Result;
            }
            catch (Exception ex)
            {
                results = new Tuple <bool, string, List <Microsoft.Graph.User> >(false, ex.Message + (ex.InnerException != null ? Environment.NewLine + ex.InnerException.Message : ""), null);
            }

            //bool test = IsUserInDirectory("*****@*****.**");

            return(View("GuestUsers", results));
        }
        public async Task <ActionResult> AssociateUserWithGroup(UserGroupAssociation model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var graphServiceClient = GraphAuthService.CreateGraphServiceClient(ConfigHelper.UseApplicationPermissions);

                    Tuple <bool, string, string, string> tup = await usersService.AssociateUserWithAGroup(graphServiceClient, model.UserEmailAddress, model.GroupName);

                    //get member of this group, which must include the new addition
                    List <ResultsItem> items = new List <ResultsItem>();

                    // Get group members.
                    IGroupMembersCollectionWithReferencesPage members = await graphServiceClient.Groups[tup.Item4].Members.Request().GetAsync();

                    if (members?.Count > 0)
                    {
                        foreach (Microsoft.Graph.User user in members)
                        {
                            // Get member properties.
                            items.Add(new ResultsItem()
                            {
                                Display = user.DisplayName, Id = user.Id
                            });
                        }
                    }

                    return(View("ListGroupMembers", new Tuple <bool, string, string, List <ResultsItem> >(tup.Item1, tup.Item2, model.GroupName, items)));
                }
                catch (Exception ex)
                {
                    model.isOk    = false;
                    model.message = ex.Message + (ex.InnerException != null ? Environment.NewLine + ex.InnerException.Message : "");
                }
            }

            return(View("AssociateUserWithGroup", model));
        }
        public async Task <ViewResult> ListAccessReviews()
        {
            string formattedJson             = "";
            string status                    = "ERROR";
            string message                   = "Could not complete the request";
            Tuple <bool, string, string> res = new Tuple <bool, string, string>(false, "", "");
            List <Tuple <string, string, string, string> > AccessReviewIDs = new List <Tuple <string, string, string, string> >();
            AccessReviews ar = new AccessReviews();
            string        masterRecurrenceType = "", childRecurrenceType = "";

            try
            {
                var graphServiceClient = GraphAuthService.CreateGraphServiceClient(ConfigHelper.UseApplicationPermissions);

                string token = GraphAuthService.GetTokenForApplication(ConfigHelper.UseApplicationPermissions, false).Result;

                res = await usersService.GetAccessReviewProgramControlList(graphServiceClient, token, ConfigHelper.GraphUrl);

                if (res.Item1)
                {
                    formattedJson = res.Item3;

                    JObject jObject = JObject.Parse(formattedJson);

                    JArray values = (JArray)jObject.SelectToken("value");//this is a list of current action reviews either recurring or not

                    foreach (JToken v in values)
                    {
                        string controlId     = (string)v.SelectToken("controlId");//refers to action review
                        string displayName   = (string)v.SelectToken("displayName");
                        string startDateTime = (string)v.SelectToken("startDateTime");
                        string endDateTime   = (string)v.SelectToken("endDateTime");
                        string arstatus      = (string)v.SelectToken("status");

                        //check if this is recurring
                        res = await usersService.GetAccessReviewDetails(graphServiceClient, token, ConfigHelper.GraphUrl, controlId);

                        if (res.Item1)
                        {
                            jObject = JObject.Parse(res.Item3);

                            masterRecurrenceType = (string)jObject.SelectToken("settings").SelectToken("recurrenceSettings").SelectToken("recurrenceType");

                            string det = "Name: " + displayName + " | Start Date: " + startDateTime + " | End Date: " + endDateTime + " | Status: " + arstatus;

                            AccessReviewIDs.Add(new Tuple <string, string, string, string>(controlId, "", masterRecurrenceType, det));

                            if (masterRecurrenceType != "onetime")
                            {
                                //get instances
                                res = await usersService.GetAccessReviewInstances(graphServiceClient, token, ConfigHelper.GraphUrl, controlId);//this would not inlcude current one

                                if (res.Item1)
                                {
                                    formattedJson = res.Item3;//this contains instance details

                                    jObject = JObject.Parse(formattedJson);

                                    JArray instances = (JArray)jObject.SelectToken("value");//instance array
                                    foreach (JToken v2 in instances)
                                    {
                                        string acid = (string)v2.SelectToken("id");
                                        startDateTime = (string)v2.SelectToken("startDateTime");
                                        endDateTime   = (string)v2.SelectToken("endDateTime");
                                        arstatus      = (string)v2.SelectToken("status");
                                        string dname = (string)v2.SelectToken("displayName");

                                        det = "Name: " + dname + " | Start Date: " + startDateTime + " | End Date: " + endDateTime + " | Status: " + arstatus;

                                        AccessReviewIDs.Add(new Tuple <string, string, string, string>(acid, controlId, "", det));
                                    }
                                }
                                else
                                {
                                    message = res.Item2;
                                }
                            }
                        }
                        else
                        {
                            message = res.Item2;
                        }
                    }

                    status = "OK";
                }
                else
                {
                    message = res.Item2;
                }
            }
            catch (Exception ex)
            {
                message = ex.Message + " " + (ex.InnerException != null ? ex.InnerException.Message : "");
            }


            return(View(new AccessReviews()
            {
                status = (status == "OK"), resultantMessage = message, lstResult = AccessReviewIDs
            }));
        }