Ejemplo n.º 1
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            try
            {
                if (HttpContext.Current.Request.Headers["authorization"] != null)
                {

                    string authString = HttpContext.Current.Request.Headers["authorization"];
                    string userId = HttpContext.Current.Request.Headers["x-jive-user-id"];

                    string[] authStringArray = authString.Split('&');
                    string tenantId = null;
                    string jiveUrl = null;
                    foreach (string authElement in authStringArray)
                    {
                        string[] keyValue = authElement.Split('=');
                        if (keyValue[0].Equals("tenant_id"))
                        {
                            tenantId = keyValue[1];
                        }

                        if (keyValue[0].Equals("jive_url"))
                        {
                            jiveUrl = HttpUtility.UrlDecode(keyValue[1]);
                        }
                    }
                    string ownerId = userId + "@" + tenantId;

                    using (JiveSdkContext db = new JiveSdkContext())
                    {
                        User myUser = null;
                        var _myUser = db.Users
                            .Include("JiveInstance.Users")
                            .Where(u => u.UserId.Equals(ownerId));
                        if (_myUser.Count() > 0)
                        {
                            myUser = _myUser.First();
                        }
                        else
                        {
                            if (myUser == null)
                            {
                                string[] jiveInstanceTemp = ownerId.Split('@');
                                String userJiveInstance = jiveInstanceTemp[1];

                                JiveInstance jiveInstanceForUser = null;


                                // Find the instance ID for the user
                                var _jiveInstanceForUser = db.JiveInstances
                                    .Include("Users")
                                    .Where(j => j.JiveInstanceId.Equals(userJiveInstance));

                                jiveInstanceForUser = _jiveInstanceForUser.First();
                                if (jiveInstanceForUser.Users == null)
                                {
                                    jiveInstanceForUser.Users = new List<User>();

                                }


                                myUser = new User();
                                myUser.DateCreated = DateTime.Now;
                                myUser.HasInstalledApp = true;
                                myUser.IsComplete = false;
                                myUser.UserId = ownerId;
                                myUser.LastUpdated = DateTime.Now;
                                //
                                db.Users.Add(myUser);
                                myUser.JiveInstance = jiveInstanceForUser;
                                db.SaveChanges();
                                jiveInstanceForUser.Users.Add(myUser);




                                db.SaveChanges();
                            }
                        }

                        GenericIdentity MyIdentity = new GenericIdentity(ownerId);

                        String[] MyStringArray = { "User" };
                        GenericPrincipal MyPrincipal =
                            new GenericPrincipal(MyIdentity, MyStringArray);
                        Thread.CurrentPrincipal = MyPrincipal;
                        return true;


                    }
                }
                else
                {

                    return false;

                }

            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                return false;
            }

        }
Ejemplo n.º 2
0
        public void SetAccess(string userId)
        {
            using (JiveSdkContext db = new JiveSdkContext())
            {
                try
                {


                    //Retrieve the current user from the database
                    //Not recommended for production where it would be more efficient
                    //to user a caching mechanism to minimize sql queries
                    var _myUser = db.Users
                                          .Include("JiveInstance")
                                          .Where(u => u.UserId == userId);
                    User myUser = null;

                    if (_myUser.Count() > 0)
                    {
                        myUser = _myUser.First();

                        if (myUser.DisplayName != null)
                        {
                            ViewBag.ownerName = myUser.DisplayName;
                        }
                        else
                        {
                            ViewBag.ownerName = "Display name not set in DB";

                        }

                        ViewBag.ownerId = userId;
                        ViewBag.jiveUrl = myUser.JiveInstance.Url;
                        string culture = Thread.CurrentThread.CurrentCulture.ToString();
 

                    }
                    else
                    {
                        //This user is not registered in the database yet

                        string[] jiveInstanceTemp = userId.Split('@');
                        String userJiveInstance = jiveInstanceTemp[1];



                        // Find the instance ID for the user
                        var _jiveInstanceForUser = db.JiveInstances
                            .Include("Users")
                            .Include("JiveInstanceSettings")
                            .Where(j => j.JiveInstanceId.Equals(userJiveInstance));

                        if (_jiveInstanceForUser.Count() == 0)
                        {
                            //The JiveInstance does not exist yet. This is a legacy setting for apps delivered via the Jive Apps market
                            isNewJiveInstance = true;
                            jiveInstanceForUser = null;
                        }
                        else
                        {
                            isNewJiveInstance = false;
                            jiveInstanceForUser = _jiveInstanceForUser.First();
                            if (jiveInstanceForUser.Users.Count() == 0)
                            {
                                jiveInstanceForUser.Users = new List<User>();

                            }
                        }



                        //Create a new JiveInstance if no one has installed the app on this system yet
                        //This is a legacy setting for apps delivered via the Jive Apps market
                        if (isNewJiveInstance == true)
                        {

                            JiveInstance jiveInstance = new JiveInstance();
                            jiveInstance.JiveInstanceId = jiveInstanceTemp[1];
                            jiveInstance.DateCreated = DateTime.Now;
                            jiveInstance.LastUpdated = DateTime.Now;
                            jiveInstance.IsComplete = false;
                            jiveInstance.Users = new List<User>();
                            jiveInstance.IsLicensed = true;
                            jiveInstance.IsInstalledViaAddon = true;
                            db.JiveInstances.Add(jiveInstance);
                            db.SaveChanges();
                            jiveInstanceForUser = jiveInstance;

                        }



                        User user = new User();
                        user.UserId = userId;
                        user.JiveInstance = jiveInstanceForUser;
                        user.DateCreated = DateTime.Now;
                        user.LastUpdated = DateTime.Now;
                        user.HasInstalledApp = true;
                        
                        db.Users.Add(user);
                        db.SaveChanges();
                        jiveInstanceForUser.Users.Add(user);
                        db.SaveChanges();

                        ViewBag.jiveUrl = user.JiveInstance.Url;
                        ViewBag.ownerName = "Display name not set in DB";
                        ViewBag.ownerId = user.UserId;

                    }


                    //Setting some helper data into the viewbag, so we can efficiently build a page

                    //The baseUrl is the system on which our addon is hosted. We need this to set the base_href for our html pages correctly
                    string baseUrl = System.Configuration.ConfigurationManager.AppSettings["baseUrl"];
                    ViewBag.baseUrl = baseUrl;

                    //The appPath is the path of our application, e.g. https://myjiveaddon.azurewebsites.net/apps/app_path
                    //We might need this to build deep links to our application
                    string appPath = System.Configuration.ConfigurationManager.AppSettings["appPath"];
                    ViewBag.appPath = appPath;

                    //Version information for app and included resources. Due to the caching mechanisms in Jive
                    //we recommend to update this when you roll out new versions
                    string jsVersion = System.Configuration.ConfigurationManager.AppSettings["jsVersion"];
                    ViewBag.jsVersion = jsVersion;
                    string appJsVersion = System.Configuration.ConfigurationManager.AppSettings["appJsVersion"];
                    ViewBag.appJsVersion = appJsVersion;


                    ViewBag.jiveUrl = myUser.JiveInstance.Url;

                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                  
                }
            }
        }