public RelationshipEventWrapper(RelationshipEvent relationshipEvent)
     : base(relationshipEvent.ID)
 {
     CreateBy = EmployeeWraper.Get(relationshipEvent.CreateBy);
     Created = (ApiDateTime)relationshipEvent.CreateOn;
     Content = relationshipEvent.Content;
     Files = new List<FileWrapper>();
     CanEdit = CRMSecurity.CanEdit(relationshipEvent);
 }
 public static bool CanEdit(RelationshipEvent relationshipEvent)
 {
     return (IsAdmin || relationshipEvent.CreateBy == SecurityContext.CurrentAccount.ID);
 }
 public static bool CanAccessTo(RelationshipEvent relationshipEvent)
 {
     return CanAccessTo((ISecurityObjectId)relationshipEvent);
 }
        public void DeleteItem(RelationshipEvent item)
        {
            CRMSecurity.DemandDelete(item);

            var relativeFiles = GetFiles(item.ID);

            var nowFilesEditing = relativeFiles.Where(file => (file.FileStatus & FileStatus.IsEditing) == FileStatus.IsEditing);

            if (nowFilesEditing.Count() != 0)
                throw new ArgumentException();

            relativeFiles.ForEach(f => RemoveFile(f));

            using (var db = GetDb())
            {
                db.ExecuteNonQuery(Delete("crm_relationship_event").Where(Exp.Eq("id", item.ID)));
            }
        }
        public static bool CanAccessTo(RelationshipEvent relationshipEvent)
        {
            if (IsAdmin)
                    return true;

            if (relationshipEvent.ContactID > 0)
            {
                var contactObj = Global.DaoFactory.GetContactDao().GetByID(relationshipEvent.ContactID);
                if (contactObj != null) return CanAccessTo(contactObj);
            }

            if (relationshipEvent.EntityType == EntityType.Case)
            {
                var caseObj = Global.DaoFactory.GetCasesDao().GetByID(relationshipEvent.EntityID);
                if (caseObj != null) return CanAccessTo(caseObj);
            }

            if (relationshipEvent.EntityType == EntityType.Opportunity)
            {
                var dealObj = Global.DaoFactory.GetDealDao().GetByID(relationshipEvent.EntityID);
                if (dealObj != null) return CanAccessTo(dealObj);
            }

            return false;

        }
        public RelationshipEventWrapper AddHistoryTo(
            String entityType,
            int entityId,
            int contactId,
            String content,
            int categoryId,
            ApiDateTime created,
            IEnumerable<int> fileId,
            IEnumerable<Guid> notifyUserList)
        {

            if (!String.IsNullOrEmpty(entityType) &&
                !(String.Compare(entityType, "opportunity", true) == 0 ||
                  String.Compare(entityType, "case", true) == 0))
                throw new ArgumentException();

            var relationshipEvent = new RelationshipEvent
                                        {
                                            CategoryID = categoryId,
                                            EntityType = ToEntityType(entityType),
                                            EntityID = entityId,
                                            Content = content,
                                            ContactID = contactId,
                                            CreateOn = created,
                                            CreateBy = Core.SecurityContext.CurrentAccount.ID
                                        };

            if (categoryId > 0)
            {
                var category = DaoFactory.GetListItemDao().GetByID(categoryId);

                if (category == null)
                    throw new ArgumentException();
            }

            var item = DaoFactory.GetRelationshipEventDao().CreateItem(relationshipEvent);

            var fileListInfoHashtable = new Hashtable();

            if (fileId != null)
            {
                var fileIds = fileId.ToList();

                var files = FilesDaoFactory.GetFileDao().GetFiles(fileIds.Cast<object>().ToArray());

                foreach (var file in files)
                {
                    var fileInfo = String.Format("{0} ({1})", file.Title, Path.GetExtension(file.Title).ToUpper());
                    fileListInfoHashtable.Add(fileInfo, file.ViewUrl);
                }

                DaoFactory.GetRelationshipEventDao().AttachFiles(item.ID, fileIds.ToArray());
            }

            if (notifyUserList != null && notifyUserList.Count() > 0)
                NotifyClient.Instance.SendAboutAddRelationshipEventAdd(item, fileListInfoHashtable, notifyUserList.ToArray());

            return ToRelationshipEventWrapper(item);
        }
 public static List<Guid> GetAccessSubjectGuidsTo(RelationshipEvent relationshipEvent)
 {
     return GetAccessSubjectGuidsTo((ISecurityObjectId)relationshipEvent);
 }
 public static bool IsPrivate(RelationshipEvent relationshipEvent)
 {
     return IsPrivate((ISecurityObjectId)relationshipEvent);
 }
        public RelationshipEventWrapper AddHistoryTo(
            string entityType,
            int entityId,
            int contactId,
            string content,
            int categoryId,
            ApiDateTime created,
            IEnumerable<int> fileId,
            IEnumerable<Guid> notifyUserList)
        {
            if (!string.IsNullOrEmpty(entityType) &&
                !(
                     string.Compare(entityType, "opportunity", StringComparison.OrdinalIgnoreCase) == 0 ||
                     string.Compare(entityType, "case", StringComparison.OrdinalIgnoreCase) == 0)
                )
                throw new ArgumentException();

            var entityTypeObj = ToEntityType(entityType);

            var entityTitle = "";
            if (contactId > 0) {
                var contact = DaoFactory.GetContactDao().GetByID(contactId);
                if (contact == null || !CRMSecurity.CanAccessTo(contact))
                    throw new ArgumentException();
                entityTitle = contact.GetTitle();
            }

            if (entityTypeObj == EntityType.Case) {
                var cases = DaoFactory.GetCasesDao().GetByID(entityId);
                if (cases == null || !CRMSecurity.CanAccessTo(cases))
                    throw new ArgumentException();
                if (contactId <= 0)
                {
                    entityTitle = cases.Title;
                }
            }
            if (entityTypeObj == EntityType.Opportunity)
            {
                var deal = DaoFactory.GetDealDao().GetByID(entityId);
                if (deal == null || !CRMSecurity.CanAccessTo(deal))
                    throw new ArgumentException();
                if (contactId <= 0)
                {
                    entityTitle = deal.Title;
                }
            }

            var relationshipEvent = new RelationshipEvent
                {
                    CategoryID = categoryId,
                    EntityType = entityTypeObj,
                    EntityID = entityId,
                    Content = content,
                    ContactID = contactId,
                    CreateOn = created,
                    CreateBy = Core.SecurityContext.CurrentAccount.ID
                };

            var category = DaoFactory.GetListItemDao().GetByID(categoryId);
            if (category == null) throw new ArgumentException();

            var item = DaoFactory.GetRelationshipEventDao().CreateItem(relationshipEvent);


            notifyUserList = notifyUserList != null ? notifyUserList.ToList() : new List<Guid>();
            var needNotify = notifyUserList.Any();

            var fileListInfoHashtable = new Hashtable();

            if (fileId != null)
            {
                var fileIds = fileId.ToList();
                var files = FilesDaoFactory.GetFileDao().GetFiles(fileIds.Cast<object>().ToArray());

                if (needNotify)
                {
                    foreach (var file in files)
                    {
                        var extension = Path.GetExtension(file.Title);
                        if (extension == null) continue;

                        var fileInfo = string.Format("{0} ({1})", file.Title, extension.ToUpper());
                        if (!fileListInfoHashtable.ContainsKey(fileInfo))
                        {
                            fileListInfoHashtable.Add(fileInfo, file.ViewUrl);
                        }
                        else
                        {
                            fileInfo = string.Format("{0} ({1}, {2})", file.Title, extension.ToUpper(), file.UniqID);
                            fileListInfoHashtable.Add(fileInfo, file.ViewUrl);
                        }
                    }
                }

                DaoFactory.GetRelationshipEventDao().AttachFiles(item.ID, fileIds.ToArray());

                if (files.Any())
                {
                    var fileAttachAction = GetFilesAttachAction(entityTypeObj, contactId);
                    MessageService.Send(Request, fileAttachAction, entityTitle, files.Select(x => x.Title));
                }
            }

            if (needNotify)
            {
                NotifyClient.Instance.SendAboutAddRelationshipEventAdd(item, fileListInfoHashtable, notifyUserList.ToArray());
            }

            var wrapper = ToRelationshipEventWrapper(item);

            var historyCreatedAction = GetHistoryCreatedAction(entityTypeObj, contactId);
            MessageService.Send(Request, historyCreatedAction, entityTitle, category.Title);

            return wrapper;
        }
        public static void DemandCreateOrUpdate(RelationshipEvent relationshipEvent)
        {
            if (String.IsNullOrEmpty(relationshipEvent.Content) || relationshipEvent.CategoryID == 0 || (relationshipEvent.ContactID == 0 && relationshipEvent.EntityID == 0))
                throw new ArgumentException();

            if (relationshipEvent.EntityID > 0 && relationshipEvent.EntityType != EntityType.Opportunity && relationshipEvent.EntityType != EntityType.Case)
                throw new ArgumentException();

            if (!CanAccessTo(relationshipEvent)) throw CreateSecurityException();
        }
        public void EditItem(RelationshipEvent item)
        {
            if (String.IsNullOrEmpty(item.Content) || item.CategoryID == 0 || (item.ContactID == 0 & item.EntityID == 0))
                throw new ArgumentException();

            if (item.EntityID > 0 && item.EntityType != EntityType.Opportunity && item.EntityType != EntityType.Case)
                throw new ArgumentException();

            CRMSecurity.DemandEdit(item);

            using (var db = GetDb())
            {
                db.ExecuteNonQuery(Update("crm_relationship_event")
                                         .Set("contact_id", item.ContactID)
                                         .Set("content", item.Content)
                                         .Set("entity_type", (int)item.EntityType)
                                         .Set("entity_id", item.EntityID)
                                         .Set("category_id", item.CategoryID)
                                         .Set("last_modifed_on", DateTime.UtcNow)
                                         .Set("last_modifed_by", ASC.Core.SecurityContext.CurrentAccount.ID)
                                         );
            }
        }
 public static void DemandDelete(RelationshipEvent relationshipEvent)
 {
    if (!CanDelete(relationshipEvent)) throw CreateSecurityException();
 }
 public static bool CanDelete(RelationshipEvent relationshipEvent)
 {
     return CanEdit(relationshipEvent);
 }
 public static bool CanEdit(RelationshipEvent relationshipEvent)
 {
     return CanAccessTo(relationshipEvent);
 }
Beispiel #15
0
 public static void SetAccessTo(RelationshipEvent relationshipEvent, List<Guid> subjectID)
 {
     SetAccessTo((ISecurityObjectId)relationshipEvent, subjectID);
 }
        private void AddToHistory(int contactID, String content)
        {

            if (contactID == 0 || String.IsNullOrEmpty(content)) return;

            var historyEvent = new RelationshipEvent();

            historyEvent.ContactID = contactID;
            historyEvent.Content = content;
            historyEvent.CreateBy = _currentUserID;
            historyEvent.CreateOn = TenantUtil.DateTimeNow();

            if (historyCategory == 0)
            {
                var listItemDao = _daoFactory.GetListItemDao();

                // HACK
                var listItem = listItemDao.GetItems(ListType.HistoryCategory).Find(
                     item => item.AdditionalParams == "event_category_email.png");

                if (listItem == null)
                    listItemDao.CreateItem(ListType.HistoryCategory, new ListItem
                                                                         {
                                                                             AdditionalParams = "event_category_email.png",
                                                                             Title = CRMCommonResource.HistoryCategory_Note
                                                                         });
                //

                historyCategory = listItem.ID;
            }

            historyEvent.CategoryID = historyCategory;

            var relationshipEventDao = _daoFactory.GetRelationshipEventDao();

            historyEvent = relationshipEventDao.CreateItem(historyEvent);

            if (historyEvent.ID > 0 && _fileID != null && _fileID.Count > 0)
                relationshipEventDao.AttachFiles(historyEvent.ID, _fileID.ToArray());
        }
Beispiel #17
0
 public static void MakePublic(RelationshipEvent relationshipEvent)
 {
     MakePublic((ISecurityObjectId)relationshipEvent);
 }
        public void SendAboutAddRelationshipEventAdd(RelationshipEvent entity,
                                                    Hashtable fileListInfoHashtable, params Guid[] userID)
        {
            if (userID.Length == 0) return;

            NameValueCollection baseEntityData;

            if (entity.EntityID != 0)
            {
                baseEntityData = ExtractBaseDataFrom(entity.EntityType, entity.EntityID);
            }
            else
            {
                var contact = Global.DaoFactory.GetContactDao().GetByID(entity.ContactID);

                baseEntityData = new NameValueCollection();
                baseEntityData["title"] = contact.GetTitle();
                baseEntityData["id"] = contact.ID.ToString();
                baseEntityData["entityRelativeURL"] = "default.aspx?id=" + contact.ID;

                if (contact is Person)
                    baseEntityData["entityRelativeURL"] += "&type=people";

                baseEntityData["entityRelativeURL"] = String.Concat(PathProvider.BaseAbsolutePath,
                                                                    baseEntityData["entityRelativeURL"]);
            }

            client.BeginSingleRecipientEvent("send about add relationship event add");

            var interceptor = new InitiatorInterceptor(new DirectRecipient(ASC.Core.SecurityContext.CurrentAccount.ID.ToString(), ""));

            client.AddInterceptor(interceptor);

            try
            {

                client.SendNoticeToAsync(
                      NotifyConstants.Event_AddRelationshipEvent,
                      null,
                      userID.Select(item => ToRecipient(item)).ToArray(),
                      true,
                      new TagValue(NotifyConstants.Tag_EntityTitle, baseEntityData["title"]),
                      new TagValue(NotifyConstants.Tag_EntityID, baseEntityData["id"]),
                      new TagValue(NotifyConstants.Tag_EntityRelativeURL, baseEntityData["entityRelativeURL"]),
                      new TagValue(NotifyConstants.Tag_AdditionalData,
                      new Hashtable { 
                      { "Files", fileListInfoHashtable },
                      {"EventContent", entity.Content}}));

            }
            finally
            {
                client.RemoveInterceptor(interceptor.Name);
                client.EndSingleRecipientEvent("send about add relationship event add");
            }


        }
Beispiel #19
0
 public static Dictionary<Guid, string> GetAccessSubjectTo(RelationshipEvent relationshipEvent)
 {
     return GetAccessSubjectTo((ISecurityObjectId)relationshipEvent);
 }
        public RelationshipEvent AttachFiles(int contactID, EntityType entityType, int entityID, int[] fileIDs)
        {
            if (entityID > 0 && !_supportedEntityType.Contains(entityType))
                throw new ArgumentException();


            var relationshipEvent = new RelationshipEvent
            {
                CategoryID = (int)HistoryCategorySystem.FilesUpload,
                ContactID = contactID,
                EntityType = entityType,
                EntityID = entityID,
                Content = HistoryCategorySystem.FilesUpload.ToLocalizedString()
            };

            relationshipEvent = CreateItem(relationshipEvent);

            AttachFiles(relationshipEvent.ID, fileIDs);

            return relationshipEvent;
        }
Beispiel #21
0
 public static void DemandAccessTo(RelationshipEvent relationshipEvent)
 {
     if (!CanAccessTo(relationshipEvent)) throw CreateSecurityException();
 }
        public RelationshipEvent CreateItem(RelationshipEvent item)
        {
            CRMSecurity.DemandCreateOrUpdate(item);

            var htmlBody = String.Empty;

            if (item.CreateOn == DateTime.MinValue)
                item.CreateOn = TenantUtil.DateTimeNow();
            item.CreateBy = ASC.Core.SecurityContext.CurrentAccount.ID;
            item.LastModifedBy = ASC.Core.SecurityContext.CurrentAccount.ID;

            if (item.CategoryID == (int)HistoryCategorySystem.MailMessage)
            {
                var jsonObj = JObject.Parse(item.Content);
                var messageId = jsonObj.Value<Int32>("message_id");

                var apiServer = new Api.ApiServer();
                var msg = apiServer.GetApiResponse(
                    String.Format("{0}mail/messages/{1}.json?id={1}&unblocked=true&is_need_to_sanitize_html=true", SetupInfo.WebApiBaseUrl, messageId), "GET");

                if (msg == null)
                    throw new ArgumentException();

                var msgResponseWrapper = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(msg)));
                var msgRequestObj = msgResponseWrapper.Value<JObject>("response");
                string messageUrl;

                htmlBody = msgRequestObj.Value<String>("htmlBody");

                using (var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlBody)))
                {
                    var filePath = String.Format("folder_{0}/message_{1}.html", (messageId/1000 + 1)*1000, messageId);

                    Global.GetStore().Save("mail_messages",filePath,fileStream);

                    messageUrl = String.Format("{0}HttpHandlers/filehandler.ashx?action=mailmessage&message_id={1}", PathProvider.BaseAbsolutePath, messageId).ToLower();

                }

                var msg_date_created = msgRequestObj.Value<String>("date");
                item.Content = JsonConvert.SerializeObject(new
                     {
                         @from = msgRequestObj.Value<String>("from"),
                         to = msgRequestObj.Value<String>("to"),
                         cc = msgRequestObj.Value<String>("cc"),
                         bcc = msgRequestObj.Value<String>("bcc"),
                         subject = msgRequestObj.Value<String>("subject"),
                         important = msgRequestObj.Value<Boolean>("important"),
                         chain_id = msgRequestObj.Value<String>("chainId"),
                         is_sended = msgRequestObj.Value<Int32>("folder") != 1,
                         date_created = msg_date_created,
                         introduction = msgRequestObj.Value<String>("introduction"),
                         message_id = msgRequestObj.Value<Int32>("id"),
                         message_url = messageUrl
                     });

                item.CreateOn = DateTime.Parse(msg_date_created, CultureInfo.InvariantCulture);
            }

            using (var db = GetDb())
            {
                item.ID = db.ExecuteScalar<int>(
                              Insert("crm_relationship_event")
                             .InColumnValue("id", 0)
                             .InColumnValue("contact_id", item.ContactID)
                             .InColumnValue("content", item.Content)
                             .InColumnValue("create_on", TenantUtil.DateTimeToUtc(item.CreateOn))
                             .InColumnValue("create_by", item.CreateBy)
                             .InColumnValue("entity_type", (int)item.EntityType)
                             .InColumnValue("entity_id", item.EntityID)
                             .InColumnValue("category_id", item.CategoryID)
                             .InColumnValue("last_modifed_on", DateTime.UtcNow)
                             .InColumnValue("last_modifed_by", item.LastModifedBy)
                             .InColumnValue("have_files", false)
                             .Identity(1, 0, true));

                if (item.CreateOn.Kind == DateTimeKind.Utc)
                    item.CreateOn = TenantUtil.DateTimeFromUtc(item.CreateOn);


                return item;
            }
        }
        private RelationshipEventWrapper ToRelationshipEventWrapper(RelationshipEvent relationshipEvent)
        {
            var result = new RelationshipEventWrapper(relationshipEvent);


            var historyCategory = DaoFactory.GetListItemDao().GetByID(relationshipEvent.CategoryID);

            if (historyCategory != null)
                result.Category = ToHistoryCategoryWrapper(historyCategory);

            if (relationshipEvent.EntityID > 0)
                result.Entity = ToEntityWrapper(relationshipEvent.EntityType, relationshipEvent.EntityID);

            result.Files = DaoFactory.GetRelationshipEventDao().GetFiles(relationshipEvent.ID).ConvertAll(file => new FileWrapper(file));

            if (relationshipEvent.ContactID > 0)
            {
                var relativeContact = DaoFactory.GetContactDao().GetByID(relationshipEvent.ContactID);

                if (relativeContact != null)
                    result.Contact = ToContactBaseWrapper(relativeContact);
            }

            result.CanEdit = CRMSecurity.CanAccessTo(relationshipEvent);

            return result;
        }
Beispiel #24
0
        public static void DemandCreateOrUpdate(RelationshipEvent relationshipEvent)
        {
            if (String.IsNullOrEmpty(relationshipEvent.Content) || relationshipEvent.CategoryID == 0 || (relationshipEvent.ContactID == 0 && relationshipEvent.EntityID == 0))
                throw new ArgumentException();

            if (relationshipEvent.EntityID > 0 && relationshipEvent.EntityType != EntityType.Opportunity && relationshipEvent.EntityType != EntityType.Case)
                throw new ArgumentException();

            if (relationshipEvent.Content.Length > Global.MaxHistoryEventCharacters)
                throw new ArgumentException(CRMErrorsResource.HistoryEventDataTooLong);

            if (!CanAccessTo(relationshipEvent)) throw CreateSecurityException();
        }