Example #1
0
        public string UpdateUsers(string UserID, string OrganizationNumber = null,
                                  string Department      = null,
                                  string FirstName       = null,
                                  string LastName        = null,
                                  string Mobile          = null,
                                  string Email           = null,
                                  string Status          = null,
                                  string AccessToBooking = null,
                                  string JPGPicture      = null)
        {
            var result = string.Empty;

            string pictureName = null;

            if (JPGPicture != null && JPGPicture != "")
            {
                pictureName = OrganizationNumber + "-" + FirstName + "-" + LastName + ".jpg";
                var fullPath = ConfigurationManager.AppSettings["RestPictureUploadFolder"] + @"\" + pictureName;
                var bytes    = Convert.FromBase64String(JPGPicture);
                try
                {
                    File.WriteAllBytes(fullPath, bytes);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            try
            {
                if (CheckUserExist(UserID)) //if exists
                {
                    if (AccessToBooking.Equals("1"))
                    {
                        //update role for booking
                        DataSet dsInDatabase = CheckUserPresentInDatabase(UserID);
                        string  PersonID     = dsInDatabase.Tables[0].Rows[0]["PersonID"].ToString();

                        UpdateUserRoleInDatabase(PersonID, Digimaker.Config.Custom.AppSettings["Role_AccessToBooking"].ToString(), 1);
                        result = "10: access to booking enabled.";
                    }
                    else if (AccessToBooking.Equals("0"))
                    {
                        //update
                        DataSet dsInDatabase = CheckUserPresentInDatabase(UserID);
                        string  PersonID     = dsInDatabase.Tables[0].Rows[0]["PersonID"].ToString();

                        UpdateUserRoleInDatabase(PersonID, Digimaker.Config.Custom.AppSettings["Role_AccessToBooking"].ToString(), 2);
                        result = "12: access to booking disabled.";
                    }

                    //Update user profile
                    var personData = PersonHandler.GetPersonData(UserID);
                    var row        = personData.Tables[0].Rows[0];

                    if (FirstName != null)
                    {
                        row["GivenName"] = FirstName;
                    }

                    if (LastName != null)
                    {
                        row["FamilyName"] = LastName;
                    }

                    if (FirstName != null || LastName != null)
                    {
                        row["DisplayName"] = FirstName + " " + LastName;
                    }

                    if (Department != null)
                    {
                        //todo: support this
                    }

                    if (Mobile != null)
                    {
                        row["MobilePhone"] = Mobile;
                    }

                    if (Email != null)
                    {
                        row["Email"] = Email;
                    }
                    if (Status == "Active" || Status == "active")
                    {
                        row["Status"] = "0";
                    }
                    else
                    {
                        //row["Status"] = "1";
                        row["Custom2"] = "hide";
                    }

                    if (pictureName != null)
                    {
                        //upload
                        var pictureID     = row["PictureID"].ToString();
                        var pictureObject = new Digimaker.Content.Picture();
                        var sizeArray     = GetPictureSizes();
                        var directory     = ConfigurationManager.AppSettings["RestPictureUploadFolder"];
                        if (pictureID != "")
                        {
                            var pictureProperty = (Digimaker.Schemas.Content.PictureData.PicturePropertyRow)PictureHandler.GetData(int.Parse(pictureID), new int[3] {
                                1, 2, 4
                            }).PictureProperty.Rows[0];
                            Picture.DeleteMain(pictureProperty.PictureMainID);
                            result += "Picture removed.";
                        }
                        var resultID = pictureObject.SavePicture(sizeArray, directory, pictureName, pictureName, "", "", "",
                                                                 int.Parse(ConfigurationManager.AppSettings["RestPictureCategory"]));
                        if (resultID != -1)
                        {
                            foreach (Digimaker.Schemas.Content.PictureData.PicturePropertyRow propertyRow in PictureHandler.GetDataOnPictureMainid(resultID, new int[3] {
                                1, 2, 4
                            }).PictureProperty.Rows)
                            {
                                if (propertyRow.IsDefault)
                                {
                                    row["PictureID"] = propertyRow.PicturePropertyID.ToString();
                                    break;
                                }
                            }
                            result += "Picture added.";
                        }
                        else
                        {
                            result += "Picture not added.";
                        }
                    }

                    PersonHandler.Update(personData, Int32.Parse(row["PersonID"].ToString()));

                    //Update User Organization In Database
                    DataSet dsInDatabase1 = CheckUserPresentInDatabase(UserID);
                    string  PersonID1     = dsInDatabase1.Tables[0].Rows[0]["PersonID"].ToString();
                    UpdateUserOrganizationInDatabase(PersonID1, OrganizationNumber, 1);

                    result = "13: user profile is updated. " + result;
                }
                else if (Email != null)  //If not exists, create user(need email).
                {
                    var organizationUnitID = CheckOrganizationNumber(OrganizationNumber);
                    if (organizationUnitID != 0)
                    {
                        //Create user
                        //AddActor(OrganizationNumber, Department, FirstName, LastName, Mobile, Email, Status, UserID);
                        Person_Add(organizationUnitID.ToString(), Department, FirstName, LastName, Mobile, Email, Status, UserID, Digimaker.Config.Custom.AppSettings["Role_AccessToBooking"].ToString(), pictureName);
                        result = "11: user created.";
                    }
                    else
                    {
                        result = "01: organization was not found.";
                    }
                }
                else
                {
                    result = "02";
                }
            }
            catch (Exception ex)
            {
                result = ex.ToString(); //todo: log it
            }

            return(result);
        }