从firebase聊天应用中停用帐户

时间:2017-12-08 04:17:09

标签: android firebase firebase-cloud-messaging firebase-storage

我在我的聊天应用程序中使用firebase数据库,当我点击停用按钮时,它会从firebase的Authentication部分删除用户,但不会从firebase的数据库部分删除用户,而我的所有用户活动都会检索用户数据库部分的数据。因此,当用户停用其帐户时,它会被删除,但他的帐户仍然可供其他用户查看。在这之下我附上我的代码..

public class SettingsActivity extends AppCompatActivity{

    ImageView ivCamera;

    private DatabaseReference mUserDatabase;
    private FirebaseUser mCurrentUser;

    private CircleImageView mDisplayImage;
    private TextView mName;
    private TextView mStatus;

    private Button mProfileBtn;
    private Button mDeactivateBtn;
    //private Button mImageBtn;

    private static final int GALLERY_PICK = 1;

    private StorageReference mImageStorage;

    private ProgressDialog mProgressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        mDisplayImage = (CircleImageView) findViewById(R.id.settings_image);
        mName = (TextView) findViewById(R.id.settings_name);
        mStatus = (TextView) findViewById(R.id.settings_status);

        mProfileBtn = (Button) findViewById(R.id.settings_profile_btn);
        mDeactivateBtn = (Button) findViewById(R.id.deactivate_btn);
        ivCamera = (ImageView) findViewById(R.id.ivCamera);
        mProgressDialog = new ProgressDialog(this);

        mImageStorage = FirebaseStorage.getInstance().getReference();

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();


        String current_uid = mCurrentUser.getUid();

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

        mUserDatabase.keepSynced(true);




        mUserDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String name = dataSnapshot.child("name").getValue().toString();
                final String image = dataSnapshot.child("image").getValue().toString();
                String status = dataSnapshot.child("status").getValue().toString();
                String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();


                mName.setText(name);
                mStatus.setText(status);



                if (!image.equals("default")) {

                    //Picasso.with(SettingsActivity.this).load(image).placeholder(R.drawable.avatar).into(mDisplayImage);
                    Picasso.with(SettingsActivity.this).load(image).networkPolicy(NetworkPolicy.OFFLINE)
                            .placeholder(R.drawable.avatar).into(mDisplayImage, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                            Picasso.with(SettingsActivity.this).load(image).placeholder(R.drawable.avatar).into(mDisplayImage);
                        }
                    });

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        //ivGallery =(ImageView) findViewById(R.id.ivGallery);

        //ivGallery.setOnClickListener(new View.OnClickListener() {
        //   @Override
        // public void onClick(View view) {

        //}
        //});

        mProfileBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name_value = mName.getText().toString();
                String status_value = mStatus.getText().toString();

                Intent status_Intent = new Intent(SettingsActivity.this, StatusActivity.class);
                status_Intent.putExtra("status_value", status_value);
                status_Intent.putExtra("name_value", name_value);

                startActivity(status_Intent);
            }

        });


        ivCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent galleryIntent = new Intent();
                galleryIntent.setType("image/*");
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(galleryIntent, "SELECT IMAGE"), GALLERY_PICK);


            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {

            Uri imageUri = data.getData();


            CropImage.activity(imageUri).setAspectRatio(1, 1).start(this);

            //Toast.makeText(SettingsActivity.this, imageUri, Toast.LENGTH_LONG).show();

        }
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {

            CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if (resultCode == RESULT_OK) {

                mProgressDialog = new ProgressDialog(SettingsActivity.this);
                mProgressDialog.setTitle("Uploading Image.....");
                mProgressDialog.setMessage("Please wait while we upload and process the image.");
                mProgressDialog.setCanceledOnTouchOutside(false);
                mProgressDialog.show();

                Uri resultUri = result.getUri();


                File thumb_filePath = new File(resultUri.getPath());

                String current_user_id = mCurrentUser.getUid();

                Bitmap thumb_bitmap = new Compressor(this)
                        .setMaxWidth(200)
                        .setMaxHeight(200)
                        .setQuality(75)
                        .compressToBitmap(thumb_filePath);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                final byte[] thumb_byte = baos.toByteArray();

                StorageReference filePath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");

                final StorageReference thumb_filepath = mImageStorage.child("profile_images").child("thumbs").child(current_user_id + ".jpg");

                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                        if (task.isSuccessful()) {

                            final String download_url = task.getResult().getDownloadUrl().toString();

                            UploadTask uploadTask = thumb_filepath.putBytes(thumb_byte);
                            uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {

                                    String thumb_downloadUrl = thumb_task.getResult().getDownloadUrl().toString();

                                    if (thumb_task.isSuccessful()) {

                                        Map update_hashMap = new HashMap();
                                        update_hashMap.put("image", download_url);
                                        update_hashMap.put("thumb_image", thumb_downloadUrl);

                                        mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {

                                                    mProgressDialog.dismiss();
                                                    Toast.makeText(SettingsActivity.this, "Success Uploading.", Toast.LENGTH_LONG).show();
                                                }
                                            }
                                        });
                                    } else {

                                        Toast.makeText(SettingsActivity.this, "Error in Uploading thumbnail.", Toast.LENGTH_LONG).show();
                                        mProgressDialog.dismiss();
                                    }
                                }
                            });


                        } else {

                            Toast.makeText(SettingsActivity.this, "Error in Uploading.", Toast.LENGTH_LONG).show();
                            mProgressDialog.dismiss();
                        }
                    }
                });

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {

                Exception error = result.getError();
            }
        }
    }


    public static String random() {
        Random generator = new Random();
        StringBuilder randomStringBuilder = new StringBuilder();
        int randomLength = generator.nextInt(10);
        char tempChar;
        for (int i = 0; i < randomLength; i++) {
            tempChar = (char) (generator.nextInt(96) + 32);
            randomStringBuilder.append(tempChar);
        }
        return randomStringBuilder.toString();
    }



            public void deactivate(View view)
            {
              FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user!=null)
            {
             mProgressDialog.setTitle("Deactivate Account");
            mProgressDialog.setMessage("Deactivating...");
              mProgressDialog.show();
              mUserDatabase.child(mCurrentUser.getUid()).removeValue();
             user.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                 public void onComplete(@NonNull Task<Void> task) {
                     if (task.isSuccessful()) {
                         Toast.makeText(getApplicationContext(), "Account is Deactivated", Toast.LENGTH_LONG).show();
                         finish();
                         Intent intent = new Intent(SettingsActivity.this, RegisterActivity.class);
                         startActivity(intent);
                     } else {
                         Toast.makeText(getApplicationContext(), "Account could not be Deactivate", Toast.LENGTH_LONG).show();
                     }
                     mProgressDialog.dismiss();
                 }
             });
              }
            }
}

我希望当用户停用其帐户时,它将不再显示在所有用户活动中

2 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点。

  1. 删除用户帐户时。您应该删除附加到该用户的所有节点。这样任何与用户相关的活动都不会被其他用户看到。!
  2. 如果您不想从数据库部分删除。您可以使用状态名称放置另一个键值。如果删除或禁用用户状态。不要在活动中获取该用户的数据。
  3. 希望这可以帮助您回答您的问题。 如果您仍然在脑海中提出疑问,可以提问。

    删除孩子:

    <input type="text" ng-model="list[0].name">
    <label ng-repeat="data in list[0].types">{{data.work}}{{$last ? '' : ', '}}</label>
    

    简单删除节点。

    您可以根据需要进行自定义。

答案 1 :(得分:0)

DatabaseReference delete = FirebaseDatabase.getInstance()。getReference()。child(“Node”);                 delete.setValue(NULL);