コード例 #1
0
        public async Task <MediaItem> AddPOIMediaItem(string tempFolder, string sourceImageFile, int chargePointId, string comment, bool isVideo, int userId)
        {
            var dataModel = new OCMEntities();

            var mediaItem = new MediaItem();

            var poi = await new POIManager().Get(chargePointId);

            if (poi == null)
            {
                //POI not recognised
                return(null);
            }
            string[] urls = await UploadPOIImageToStorage(tempFolder, sourceImageFile, poi);

            if (urls == null)
            {
                //failed to upload, preserve submission data
                var outputPath = Path.Join(tempFolder, "OCM_" + chargePointId + "_" + (DateTime.Now.ToFileTimeUtc().ToString()) + ".json");
                System.IO.File.WriteAllText(outputPath, "{userId:" + userId + ",comment:\"" + comment + "\"}");
                return(null);
            }
            else
            {
                mediaItem.ItemUrl          = urls[0];
                mediaItem.ItemThumbnailUrl = urls[1];

                mediaItem.User               = dataModel.Users.FirstOrDefault(u => u.Id == userId);
                mediaItem.ChargePoint        = dataModel.ChargePoints.FirstOrDefault(cp => cp.Id == chargePointId);
                mediaItem.Comment            = comment;
                mediaItem.DateCreated        = DateTime.UtcNow;
                mediaItem.IsEnabled          = true;
                mediaItem.IsExternalResource = false;
                mediaItem.IsVideo            = isVideo;

                dataModel.MediaItems.Add(mediaItem);

                dataModel.ChargePoints.Find(chargePointId).DateLastStatusUpdate = DateTime.UtcNow;
                dataModel.SaveChanges();

                new UserManager().AddReputationPoints(userId, 1);

                try
                {
                    //fire and forget cache update
                    CacheManager.RefreshCachedPOI(poi.ID);
                }
                catch { }

                return(mediaItem);
            }
        }
コード例 #2
0
        public void DeleteMediaItem(int userId, int mediaItemId)
        {
            var dataModel = new OCMEntities();

            var item = dataModel.MediaItems.FirstOrDefault(c => c.ID == mediaItemId);

            if (item != null)
            {
                var cpID = item.ChargePointID;
                dataModel.MediaItems.Remove(item);
                dataModel.ChargePoints.Find(cpID).DateLastStatusUpdate = DateTime.UtcNow;
                dataModel.SaveChanges();

                //TODO: delete from underlying storage
                var user = new UserManager().GetUser(userId);
                AuditLogManager.Log(user, AuditEventType.DeletedItem, "{EntityType:\"Comment\", EntityID:" + mediaItemId + ",ChargePointID:" + cpID + "}", "User deleted media item");
            }
        }
コード例 #3
0
        public MediaItem AddPOIMediaItem(string tempFolder, string sourceImageFile, int chargePointId, string comment, bool isVideo, int userId)
        {
            var dataModel = new OCMEntities();

            var mediaItem = new MediaItem();

            var poi = new POIManager().Get(chargePointId);

            string[] urls = UploadPOIImageToStorage(tempFolder, sourceImageFile, poi);
            if (urls == null)
            {
                //failed to upload, preserve submission data
                System.IO.File.WriteAllText(tempFolder + "//OCM_" + chargePointId + "_" + (DateTime.Now.ToFileTimeUtc().ToString()) + ".json", "{userId:" + userId + ",comment:\"" + comment + "\"}");
                return(null);
            }
            else
            {
                mediaItem.ItemURL          = urls[0];
                mediaItem.ItemThumbnailURL = urls[1];

                mediaItem.User               = dataModel.Users.FirstOrDefault(u => u.ID == userId);
                mediaItem.ChargePoint        = dataModel.ChargePoints.FirstOrDefault(cp => cp.ID == chargePointId);
                mediaItem.Comment            = comment;
                mediaItem.DateCreated        = DateTime.UtcNow;
                mediaItem.IsEnabled          = true;
                mediaItem.IsExternalResource = false;
                mediaItem.IsVideo            = isVideo;

                dataModel.MediaItems.Add(mediaItem);

                dataModel.ChargePoints.Find(chargePointId).DateLastStatusUpdate = DateTime.UtcNow;
                dataModel.SaveChanges();

                new UserManager().AddReputationPoints(userId, 1);

                return(mediaItem);
            }
        }
コード例 #4
0
        private ActionResult ProcessLoginResult(string userIdentifier, string loginProvider, string name, string email)
        {
            //TODO: move all of this logic to UserManager
            string newSessionToken = Guid.NewGuid().ToString();

            using (var dataModel = new OCMEntities())
            {
                var userDetails = dataModel.Users.FirstOrDefault(u => u.Identifier.ToLower() == userIdentifier.ToLower() && u.IdentityProvider.ToLower() == loginProvider.ToLower());
                if (userDetails == null)
                {
                    //create new user details
                    userDetails = new Core.Data.User();
                    userDetails.IdentityProvider = loginProvider;
                    userDetails.Identifier       = userIdentifier;
                    if (String.IsNullOrEmpty(name) && loginProvider.ToLower() == "twitter")
                    {
                        name = userIdentifier;
                    }
                    if (String.IsNullOrEmpty(name) && email != null)
                    {
                        name = email.Substring(0, email.IndexOf("@"));
                    }

                    userDetails.Username        = name;
                    userDetails.EmailAddress    = email;
                    userDetails.DateCreated     = DateTime.UtcNow;
                    userDetails.DateLastLogin   = DateTime.UtcNow;
                    userDetails.IsProfilePublic = true;

                    //only update session token if new (also done on logout)
                    if (String.IsNullOrEmpty(userDetails.CurrentSessionToken))
                    {
                        userDetails.CurrentSessionToken = newSessionToken;
                    }

                    dataModel.Users.Add(userDetails);
                }
                else
                {
                    //update date last logged in and refresh users details if more information provided
                    userDetails.DateLastLogin = DateTime.UtcNow;

                    if (userDetails.Username == userDetails.Identifier && !String.IsNullOrEmpty(name))
                    {
                        userDetails.Username = name;
                    }
                    if (String.IsNullOrEmpty(userDetails.EmailAddress) && !String.IsNullOrEmpty(email))
                    {
                        userDetails.EmailAddress = email;
                    }

                    //only update session token if new (also done on logout)
                    if (String.IsNullOrEmpty(userDetails.CurrentSessionToken))
                    {
                        userDetails.CurrentSessionToken = newSessionToken;
                    }
                }

                //get whichever session token we used
                //newSessionToken = userDetails.CurrentSessionToken;

                //store updates to user
                dataModel.SaveChanges();

                PerformCoreLogin(OCM.API.Common.Model.Extensions.User.FromDataModel(userDetails));

                /*if (HttpContext.Items.ContainsKey("_postLoginRedirect"))
                 * {
                 *  string returnURL = HttpContext.Items["_postLoginRedirect"].ToString();
                 *  return Redirect(returnURL);
                 * }*/

                if (!String.IsNullOrEmpty(Session.GetString("_redirectURL")))
                {
                    string returnURL = Session.GetString("_redirectURL");
                    return(Redirect(returnURL));
                }
                else
                {
                    //nowhere specified to redirect to, redirect to home page
                    return(RedirectToAction("Index", "Home"));
                }
            }
        }